Documentation
¶
Overview ¶
Package logger provides structured logging with multiple backend implementations.
Use New() or NewFromConfig() to create a logger instance. The factory pattern allows selecting between zap-backed or stdlib slog implementations at runtime.
Example:
l, err := logger.New(logger.KindZap, logger.WithLevel("debug"))
if err != nil {
log.Fatal(err)
}
l.Info("application started")
Shared utilities (WithCorrelationID, CorrelationID, RedactPII) are available from this package.
Index ¶
- func CorrelationID(ctx context.Context) string
- func New(kind Kind, opts ...options.Option[Config]) (interfaces.Logger, error)
- func NewFromConfig(cfg Config) (interfaces.Logger, error)
- func RedactPII(s string) string
- func SpanID(ctx context.Context) string
- func TraceID(ctx context.Context) string
- func WithAddSource(enabled bool) options.Option[Config]
- func WithCorrelationID(ctx context.Context, id string) context.Context
- func WithFormat(format string) options.Option[Config]
- func WithLevel(level string) options.Option[Config]
- func WithRedactPII(enabled bool) options.Option[Config]
- type Config
- type Kind
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CorrelationID ¶
CorrelationID extracts the correlation ID from context. Returns empty string if no correlation ID is present.
func New ¶
New creates a Logger of the specified kind with optional functional options. Returns an error if the kind is unknown.
func NewFromConfig ¶
func NewFromConfig(cfg Config) (interfaces.Logger, error)
NewFromConfig creates a Logger from a Config struct. Returns an error if the kind is unknown.
func RedactPII ¶
RedactPII replaces PII patterns (email, phone, SSN, IPv4, auth tokens) with "[REDACTED]". Use this to sanitize log messages before recording them.
func SpanID ¶
SpanID extracts the OpenTelemetry span ID from context. Returns the hex-encoded span ID string if a valid span context is present, empty string otherwise. This enables drill-down from log entries to specific spans within a trace.
func TraceID ¶
TraceID extracts the OpenTelemetry trace ID from context. Returns the hex-encoded trace ID string if a valid span context is present, empty string otherwise. This enables log-trace correlation in Grafana (Loki → Tempo linking).
func WithAddSource ¶
WithAddSource enables or disables source code location in log records. Only used when Kind is KindStdlib.
func WithCorrelationID ¶
WithCorrelationID returns a context with the given correlation ID. Use this to propagate request tracing IDs through the call stack.
func WithFormat ¶
WithFormat sets the log format (json or text). Only used when Kind is KindZap.
Types ¶
type Config ¶
type Config struct {
// Level sets the minimum log severity (debug, info, warn, error).
// Applies to all logger kinds.
Level string
// Format selects the output encoding: "json" or "text".
// Only used when Kind is KindZap, ignored for KindStdlib.
Format string
// Kind specifies which logger implementation to use.
Kind Kind
// RedactPII enables automatic PII scrubbing.
// Only used when Kind is KindZap, ignored for KindStdlib.
RedactPII bool
// AddSource enables source code location in log records.
// Only used when Kind is KindStdlib, ignored for KindZap.
AddSource bool
}
Config is the superset configuration for all logger kinds. Kind-incompatible fields are silently ignored (e.g., Format and RedactPII are ignored when Kind=KindStdlib).
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns the default logger configuration with KindZap and sensible defaults.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package logctx provides shared context utilities for logger sub-packages.
|
Package logctx provides shared context utilities for logger sub-packages. |
|
Package stdlib provides a pure stdlib slog logger implementation.
|
Package stdlib provides a pure stdlib slog logger implementation. |
|
Package zap provides a zap-backed logger implementation with PII redaction.
|
Package zap provides a zap-backed logger implementation with PII redaction. |