logger

package
v0.21.0 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2026 License: AGPL-3.0 Imports: 5 Imported by: 0

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

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

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

func ContextWithoutRequestID(ctx context.Context) context.Context

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

func New(w io.Writer, level, format string) *slog.Logger

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

func RequestIDFromContext(ctx context.Context) string

RequestIDFromContext reports the request identifier ctx carries, or "".

func SetDefault added in v0.21.0

func SetDefault(l *slog.Logger)

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.

Jump to

Keyboard shortcuts

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