Code-Memo

log and log/slog

Simple logger
import "log"

log.Println("info")
log.Fatal(err) // exits
Flags and prefix
log.SetFlags(log.LstdFlags | log.Lshortfile)
log.SetPrefix("api: ")
Structured logging (slog, Go 1.21+)
import "log/slog"

slog.Info("request", "method", "GET", "path", "/health", "ms", 3)
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
logger.Error("failed", "err", err)
Levels

Debug, Info, Warn, Error — map to handlers and slog.Level thresholds.

Context attributes

Use slog.With or pass context.Context with handlers that read trace IDs from context.

Performance

Precompute static attribute slices; avoid expensive String() in hot Debug paths — or gate with level checks.

Third-party

zap and zerolog remain popular for maximum throughput; slog is enough for many services.