Skip to main content
Platforma provides structured logging built on Go’s standard 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:
Each function accepts a message string followed by key-value pairs for structured data.

Context-Aware Logging

Use context variants to automatically include context values:
Context logging automatically extracts and includes:
  • 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 (usually os.Stdout or os.Stderr)
  • loggerType string: Format type - “json” or “text”
  • level slog.Level: Minimum level - LevelDebug, LevelInfo, LevelWarn, LevelError
  • contextKeys 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:
Levels in order:
  • LevelDebug - Most verbose, for development
  • LevelInfo - General information, default for production
  • LevelWarn - Warning conditions
  • LevelError - 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

Always use InfoContext, ErrorContext, etc. when you have a context available. This enables automatic enrichment:
Pass data as key-value pairs, not in the message:
  • Debug: Detailed information for debugging (disabled in production)
  • Info: General informational messages about normal operation
  • Warn: Warning conditions that should be reviewed
  • Error: Error conditions that need attention
JSON format is better for log aggregation systems like ELK, Datadog, etc.:

Complete Example

Output (JSON format):