Documentation
¶
Overview ¶
Package logging provides a context-aware slog handler that automatically extracts request-scoped attributes from the context and appends them to every log record. This gives all log lines within an HTTP request a shared correlation ID without callers needing to pass it explicitly.
Two layers of context are supported:
- ContextAttrs — set once by the logging middleware with the request ID.
- Extractors — optional functions registered at init time that pull additional fields (user_id, tenant_id) from context. This avoids an import dependency from logging → auth while still enriching every log line with identity information set by later middleware.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type ContextAttrs ¶
type ContextAttrs struct {
RequestID string
}
ContextAttrs are the request-scoped attributes injected by the logging middleware and automatically attached to every slog record.
func AttrsFromContext ¶
func AttrsFromContext(ctx context.Context) ContextAttrs
AttrsFromContext retrieves the request-scoped logging attributes, or returns a zero value if none are set.
type ContextHandler ¶
type ContextHandler struct {
// contains filtered or unexported fields
}
ContextHandler wraps an slog.Handler and enriches every log record with request-scoped attributes from the context.
func NewContextHandler ¶
func NewContextHandler(inner slog.Handler, extractors ...Extractor) *ContextHandler
NewContextHandler creates a new ContextHandler wrapping the given handler. Optional extractors are called on every log record to pull additional attributes from context (e.g. user_id, tenant_id from auth middleware).