Documentation
¶
Overview ¶
Package logger builds the process's slog logger.
The rules this package exists to enforce:
- A logger is constructed and passed, never reached for. There is no package-level logger variable and no logging functions here. Code that logs takes a *slog.Logger the way it takes any other dependency.
- The context carries request data, not the logger. slog's own API took the opposite direction on purpose: Handler.Handle receives a context so a handler can read values that are already there, which is what correlation.Handler below does.
- Output keys are snake_case, including the correlation fields. Log records are not OpenTelemetry attributes; renaming them to dotted form would only collide with the underscore-flattening most log pipelines apply on ingest.
Nothing here writes to os.Stdout by itself. Init used to, which is why this package had no test that could look at its output.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ContextWithRequestID ¶ added in v0.21.0
ContextWithRequestID returns ctx carrying id, so that every record logged with that context reports it.
The request identifier already leaves this process in error responses (apperror.Problem carries request_id). Putting it on log records is what makes an identifier a user can quote back useful: without it, the identifier names something the logs cannot find.
func ContextWithoutRequestID ¶ added in v0.21.0
ContextWithoutRequestID returns ctx without the request identifier it carries, for work that outlives the request and is not part of it.
func New ¶ added in v0.21.0
New builds a logger writing to w. format selects the encoding ("json" for JSON, anything else for text); level is one of debug, info, warn, error and falls back to info.
The returned logger already carries the correlation handler, so any record logged with a context that has a request or trace identity picks those fields up without the call site repeating them.
func RequestIDFromContext ¶ added in v0.21.0
RequestIDFromContext reports the request identifier ctx carries, or "".
func SetDefault ¶ added in v0.21.0
SetDefault points slog's package-level logger, and with it the standard log package, at l.
This exists for output that is not ours: a dependency calling log.Printf, or slog.Default() from a library. Call it once during startup. It is not the way this codebase logs — application code holds a *slog.Logger.
Types ¶
This section is empty.