Structured logger backed by log/slog with request-context enrichment, extra-field context helpers, and duck-typed automatic error enrichment. What's included: - `Logger` interface with Debug / Info / Warn / Error / With / WithContext; `New(Options)` constructor writing to os.Stdout - `WithRequestID` / `GetRequestID` and `WithField` / `WithFields` context helpers — package owns both context keys - Automatic error_code and context-field enrichment in Logger.Error via duck-typed errorWithCode / errorWithContext interfaces (no xerrors import) Tested-via: todo-api POC integration Reviewed-against: docs/adr/
27 lines
855 B
Go
27 lines
855 B
Go
// Package logz provides structured logging backed by [log/slog].
|
|
//
|
|
// Create a logger with [New]:
|
|
//
|
|
// logger := logz.New(logz.Options{
|
|
// Level: slog.LevelDebug,
|
|
// JSON: true,
|
|
// StaticArgs: []any{"service", "api", "env", "production"},
|
|
// })
|
|
//
|
|
// Log at any level:
|
|
//
|
|
// logger.Info("server started", "port", 8080)
|
|
// logger.Error("request failed", err, "path", "/users")
|
|
//
|
|
// Errors that implement ErrorCode() and ErrorContext() are automatically
|
|
// enriched — the error code and context fields are added to the log record
|
|
// without any extra method calls. This pairs naturally with xerrors.Err.
|
|
//
|
|
// Attach request context to a child logger:
|
|
//
|
|
// ctx = logz.WithRequestID(ctx, requestID)
|
|
// ctx = logz.WithField(ctx, "user_id", userID)
|
|
// reqLogger := logger.WithContext(ctx)
|
|
// reqLogger.Info("handling request")
|
|
package logz
|