Documentation
¶
Overview ¶
Package ctxlog provides context-aware structured logging helpers for Go's log/slog.
Index ¶
- func From(ctx context.Context, logger *slog.Logger) *slog.Logger
- func Logger(ctx context.Context) *slog.Logger
- func Middleware(next http.Handler) http.Handler
- func MiddlewareWithHeader(headerName string) func(http.Handler) http.Handler
- func RequestID(ctx context.Context) string
- func With(ctx context.Context, args ...any) context.Context
- func WithAttrs(ctx context.Context, attrs ...slog.Attr) context.Context
- func WithRequestID(ctx context.Context, id string) context.Context
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func From ¶
From enriches the provided logger with all fields, attributes, and request ID stored in the context.
func Logger ¶
Logger returns slog.Default() enriched with all fields, attributes, and request ID stored in the context.
func Middleware ¶
Middleware generates a UUID v4 request ID, injects it into the request context via WithRequestID, and sets the X-Request-ID response header.
func MiddlewareWithHeader ¶
MiddlewareWithHeader returns middleware that reads the request ID from the specified header. If the header is present and non-empty, its value is used; otherwise a new UUID v4 is generated. The request ID is injected into the context and set as the X-Request-ID response header.
func RequestID ¶
RequestID extracts the request ID from the context. Returns an empty string if no request ID has been set.
func With ¶
With attaches slog key-value pairs to the context. The args should be alternating key-value pairs as accepted by slog.Logger.With. Multiple calls to With accumulate fields.
Types ¶
This section is empty.