log/slog package with automatic context enrichment.
Overview
The logging system provides:- Structured logging with key-value pairs
- Context-aware logging with automatic field injection
- Multiple output formats (JSON, text)
- Configurable log levels
- Trace ID support for request correlation
- Integration with other Platforma components
Basic Usage
Use the package-level functions for simple logging:Context-Aware Logging
Use context variants to automatically include context values:- Trace ID
- User ID
- Worker ID
- Domain name
- Service name
- Startup task name
Context Keys
Platforma defines standard context keys for enrichment:Adding Context Values
Add context values to enable automatic enrichment:Trace IDs
Trace IDs enable request correlation across logs:Manual Trace ID
Automatic Trace ID with Middleware
Platforma’s HTTP server includes trace ID middleware:Creating a Logger
Create a custom logger with specific configuration:Logger Parameters
w io.Writer: Output destination (usuallyos.Stdoutoros.Stderr)loggerType string: Format type - “json” or “text”level slog.Level: Minimum level -LevelDebug,LevelInfo,LevelWarn,LevelErrorcontextKeys map[string]any: Additional context keys to extract
Setting the Default Logger
Replace the default logger used by package functions:Log Levels
Control log verbosity with levels:LevelDebug- Most verbose, for developmentLevelInfo- General information, default for productionLevelWarn- Warning conditionsLevelError- Error conditions
Output Formats
Text Format
Human-readable format for development:JSON Format
Structured format for log aggregation:Integration with Platforma Components
Queue Workers
Worker IDs are automatically added:Scheduler Tasks
Trace IDs are automatically added to each run:Auth Middleware
User IDs are automatically added:Best Practices
Use context variants
Use context variants
Always use
InfoContext, ErrorContext, etc. when you have a context available. This enables automatic enrichment:Use structured fields
Use structured fields
Pass data as key-value pairs, not in the message:
Choose the right level
Choose the right level
Debug: Detailed information for debugging (disabled in production)Info: General informational messages about normal operationWarn: Warning conditions that should be reviewedError: Error conditions that need attention
Use JSON in production
Use JSON in production
JSON format is better for log aggregation systems like ELK, Datadog, etc.: