Overview
The logging system provides structured logging using PocketBase’s logger with automatic request tracking, trace ID generation, and panic recovery.Functions
SetupLogging
*server.Server
required
Server instance to configure
core/logging/logging.go:118
Features:
- Creates application logger with common fields (PID, start time)
- Logs application startup and shutdown events
- Sets up request middleware for all HTTP requests
- Generates unique trace IDs for each request
- Tracks request metrics (duration, status, rate)
- Excludes common static file requests from logs
- Sets up error handler and panic recovery
SetupRecovery
core.App
required
PocketBase application
*core.ServeEvent
required
ServeEvent to bind recovery middleware
core/logging/logging.go:227
Example:
Context Logging
InfoWithContext
context.Context
Request context (optional, for request ID extraction)
core.App
required
PocketBase application
string
required
Log message
map[string]interface{}
required
Additional structured data
core/logging/logging.go:75
Example:
ErrorWithContext
context.Context
Request context (optional)
core.App
required
PocketBase application
string
required
Error message
error
required
Error object
map[string]any
required
Additional structured data
core/logging/logging.go:94
Example:
Log Levels
core/logging/logging.go:18
Log Context
core/logging/logging.go:48
Request Middleware Behavior
Trace ID Generation
Every request receives a unique 18-character trace ID:X-Trace-ID
Example:
Request Tracking
The middleware tracks:- Request method
- Request path
- Status code
- Duration
- User agent
- Remote IP
- Content length
- Request rate (requests per second)
core/logging/logging.go:168
Excluded Paths
These paths are excluded from logging and metrics:/service-worker.js/favicon.ico/manifest.json/robots.txt- Files ending in:
.map,.ico,.webmanifest
core/logging/logging.go:60
Complete Examples
Basic Setup
Handler with Context Logging
Custom Middleware with Logging
Structured Error Logging
Log Output Examples
Application Startup
Request Log
Application Shutdown
Constants
core/logging/logging.go:27
Best Practices
- Use Context Logging: Always use
InfoWithContextandErrorWithContextin handlers - Include Trace IDs: Trace IDs help correlate logs across distributed operations
- Structured Data: Use key-value pairs instead of string interpolation
- Error Context: Always include relevant context when logging errors
- Avoid PII: Don’t log passwords, tokens, or sensitive personal information
- Consistent Keys: Use consistent field names across logs (
user_id, notuserIdoruser) - Log Levels: Use appropriate levels (Debug for verbose, Info for flow, Error for problems)
Related
- Error Handler - Structured error handling
- Recovery - Panic recovery middleware
- Monitoring - Request metrics and system stats