Skip to main content

Overview

The logging system provides structured logging using PocketBase’s logger with automatic request tracking, trace ID generation, and panic recovery.

Functions

SetupLogging

Configures logging and request middleware for the server.
*server.Server
required
Server instance to configure
Location: core/logging/logging.go:118 Features:
  1. Creates application logger with common fields (PID, start time)
  2. Logs application startup and shutdown events
  3. Sets up request middleware for all HTTP requests
  4. Generates unique trace IDs for each request
  5. Tracks request metrics (duration, status, rate)
  6. Excludes common static file requests from logs
  7. Sets up error handler and panic recovery
Example:

SetupRecovery

Configures panic recovery middleware.
core.App
required
PocketBase application
*core.ServeEvent
required
ServeEvent to bind recovery middleware
Location: core/logging/logging.go:227 Example:

Context Logging

InfoWithContext

Logs an info message with context data.
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
Location: core/logging/logging.go:75 Example:

ErrorWithContext

Logs an error message with context data.
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
Location: core/logging/logging.go:94 Example:

Log Levels

Location: core/logging/logging.go:18

Log Context

Location: core/logging/logging.go:48

Request Middleware Behavior

Trace ID Generation

Every request receives a unique 18-character trace ID:
Header: 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)
Location: 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
Location: 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

Location: core/logging/logging.go:27

Best Practices

  1. Use Context Logging: Always use InfoWithContext and ErrorWithContext in handlers
  2. Include Trace IDs: Trace IDs help correlate logs across distributed operations
  3. Structured Data: Use key-value pairs instead of string interpolation
  4. Error Context: Always include relevant context when logging errors
  5. Avoid PII: Don’t log passwords, tokens, or sensitive personal information
  6. Consistent Keys: Use consistent field names across logs (user_id, not userId or user)
  7. Log Levels: Use appropriate levels (Debug for verbose, Info for flow, Error for problems)