Browse the docs

Logging

go.putnami.dev/logger provides structured logging with pluggable sinks, context-aware propagation, and output formats compatible with Google Cloud Logging.

Default logger

import "go.putnami.dev/logger"

log := logger.Default()
log.Info("server started")
log.Error("connection failed", err)

The default logger reads its level live, on each record:

Setting Description
logger.level (config) Minimum log level: debug, info, warn, error
logger.refreshSeconds (config) How often a started app re-reads logger.level from its conf files and CONFIG_DATA (default 30; 0 turns it off)
LOG_LEVEL (env) The level when logger.level is absent (default: info)
K_SERVICE (env) When set (Cloud Run), uses JSON sink automatically

A change to logger.level applies without a restart, and logger.SetLevel changes the level at runtime; the last change wins. logger.SetLevelLimit adds a process-wide floor that every logger consults on each record, without changing a level: the telemetry plugin sets one from an OTLP export directive's log-level for its ttl. A record at error is always emitted. Every record counts once in the telemetry counter log.records{level, outcome}.

The default logger writes JSON to stdout until a route holds. logger.RouteDefault sends the records of the default logger, and of every logger derived from it before or after, to one sink instead; a record its predicate keeps also reaches stdout, and the last release restores stdout, then flushes and closes the sink. The telemetry the environment turns on (telemetry.FromEnvironment) holds that route to an OTLP log sink while the application runs, keeping putnami.ready readiness records on stdout. A logger logger.New builds keeps its own sinks.

Creating loggers

log := logger.New("my-service", logger.LevelInfo,
    logger.NewJSONSink(),
)

// With console output
log := logger.New("my-service", logger.LevelDebug,
    logger.NewConsoleSink(),
)

Log levels

type Level int

const (
    LevelDebug Level = iota
    LevelInfo
    LevelWarn
    LevelError
)

Parse from strings:

level := logger.ParseLevel("debug") // LevelDebug
level := logger.ParseLevel("info")  // LevelInfo
level := logger.ParseLevel("warn")  // LevelWarn
level := logger.ParseLevel("error") // LevelError

Logging methods

log.Debug("processing request")
log.Info("user created")
log.Warn("deprecated API called")
log.Error("query failed", err)

Structured context

Add persistent fields to a logger:

// Named child logger
requestLog := log.Named("http")
// Output: [my-service.http] ...

// Add key-value context
userLog := log.With("userId", "user-123", "requestId", "req-456")
userLog.Info("processing order")
// Output includes userId=user-123 requestId=req-456

Child loggers inherit the parent's sinks and level but add their own context fields.

Sinks

JSON sink

Structured JSON output compatible with Google Cloud Logging:

sink := logger.NewJSONSink()

Output format:

{"severity":"INFO","message":"server started","timestamp":"2024-01-15T10:30:00Z","logger":"my-service"}

Console sink

Human-readable text output for local development:

sink := logger.NewConsoleSink()

Output format:

2024-01-15 10:30:00 INFO  [my-service] server started

Memory sink

In-memory sink for testing:

sink := logger.NewMemorySink()

log := logger.New("test", logger.LevelDebug, sink)
log.Info("test message")

// Inspect entries
fmt.Println(sink.Len())            // 1
fmt.Println(sink.Last().Message)   // "test message"

// All entries
for _, entry := range sink.Entries {
    fmt.Println(entry.Level, entry.Message)
}

// Clear
sink.Clear()

Context integration

Attach a logger to context.Context for propagation through the call chain:

// Attach to context
ctx := logger.WithLogger(ctx, log.With("requestId", requestID))

// Retrieve from context
func processRequest(ctx context.Context) {
    log := logger.FromContext(ctx)
    if log != nil {
        log.Info("processing")
    }

    // Or with fallback to default
    log = logger.FromContextOrDefault(ctx)
    log.Info("processing")
}

Patterns

Per-request logging

func requestLogger() fhttp.Middleware {
    return func(ctx *fhttp.Context, next fhttp.Handler) *fhttp.Response {
        log := logger.Default().
            With("method", ctx.Method).
            With("path", ctx.Path).
            With("requestId", ctx.Header("X-Request-ID"))

        // Attach to context for downstream use
        ctx.SetContext(logger.WithLogger(ctx.Context(), log))

        start := time.Now()
        resp := next(ctx)
        log.Info(fmt.Sprintf("completed in %dms", time.Since(start).Milliseconds()))
        return resp
    }
}

Service-level logging

type UserService struct {
    log  *logger.Logger
    repo *UserRepository
}

func NewUserService(repo *UserRepository) *UserService {
    return &UserService{
        log:  logger.Default().Named("UserService"),
        repo: repo,
    }
}

func (s *UserService) Create(ctx context.Context, input CreateInput) (*User, error) {
    s.log.Info("creating user")
    user, err := s.repo.Create(ctx, input)
    if err != nil {
        s.log.Error("failed to create user", err)
        return nil, err
    }
    s.log.Info("user created")
    return user, nil
}

Support and compatibility

go.putnami.dev/logger is a public, documented, maintained package classified stable. Its structured-logging specification and accepted record-precedence ADR live next to the package source. Before v1.0.0, minor 0.x releases may still contain documented breaking changes; strict compatibility across every pre-1.0 minor or blanket cross-runtime parity is not promised.