logger

package
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 11, 2026 License: MIT Imports: 8 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func CorrelationID

func CorrelationID(ctx context.Context) string

CorrelationID extracts the correlation ID from context. Returns empty string if no correlation ID is present.

func New

func New(kind Kind, opts ...options.Option[Config]) (interfaces.Logger, error)

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

func RedactPII(s string) string

RedactPII replaces PII patterns (email, phone, SSN, IPv4, auth tokens) with "[REDACTED]". Use this to sanitize log messages before recording them.

func SpanID

func SpanID(ctx context.Context) string

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

func TraceID(ctx context.Context) string

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

func WithAddSource(enabled bool) options.Option[Config]

WithAddSource enables or disables source code location in log records. Only used when Kind is KindStdlib.

func WithCorrelationID

func WithCorrelationID(ctx context.Context, id string) context.Context

WithCorrelationID returns a context with the given correlation ID. Use this to propagate request tracing IDs through the call stack.

func WithFormat

func WithFormat(format string) options.Option[Config]

WithFormat sets the log format (json or text). Only used when Kind is KindZap.

func WithLevel

func WithLevel(level string) options.Option[Config]

WithLevel sets the log level.

func WithRedactPII

func WithRedactPII(enabled bool) options.Option[Config]

WithRedactPII enables or disables PII redaction. 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.

func (Config) ToOptions

func (c Config) ToOptions() []options.Option[Config]

ToOptions converts this Config to a slice of Option functions. Useful for converting configs to builder options.

type Kind

type Kind int

Kind specifies which logger implementation to use.

const (
	// KindZap uses a zap-backed logger with PII redaction.
	// Suitable for production with structured JSON logging.
	KindZap Kind = iota

	// KindStdlib uses pure stdlib slog logger.
	// Suitable for environments where zero external dependencies are required.
	KindStdlib
)

func (Kind) String

func (k Kind) String() string

String returns the string representation of Kind.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL