Documentation
¶
Overview ¶
Package ctxlog provides context-aware structured logging helpers for Go's log/slog.
Index ¶
- func CorrelationID(ctx context.Context) string
- func Fields(ctx context.Context) []slog.Attr
- 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 MiddlewareWithConfig(cfg MiddlewareConfig) func(http.Handler) http.Handler
- func MiddlewareWithHeader(headerName string) func(http.Handler) http.Handler
- func RequestID(ctx context.Context) string
- func TraceID(ctx context.Context) string
- func With(ctx context.Context, args ...any) context.Context
- func WithAttrs(ctx context.Context, attrs ...slog.Attr) context.Context
- func WithCorrelationID(ctx context.Context, id string) context.Context
- func WithRequestID(ctx context.Context, id string) context.Context
- func WithTraceID(ctx context.Context, id string) context.Context
- type MiddlewareConfig
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CorrelationID ¶ added in v0.2.0
CorrelationID extracts the correlation ID from the context. Returns an empty string if no correlation ID has been set.
func Fields ¶ added in v0.2.0
Fields extracts all typed slog.Attr values stored in the context via WithAttrs. Returns nil if no attributes have been set.
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 MiddlewareWithConfig ¶ added in v0.2.0
func MiddlewareWithConfig(cfg MiddlewareConfig) func(http.Handler) http.Handler
MiddlewareWithConfig returns middleware configured with the provided options. Zero-value fields use sensible defaults: HeaderName defaults to "X-Request-ID" and GenerateID defaults to UUID v4 generation.
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 TraceID ¶ added in v0.2.0
TraceID extracts the trace ID from the context. Returns an empty string if no trace 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.
func WithAttrs ¶
WithAttrs attaches typed slog.Attr values to the context. Multiple calls to WithAttrs accumulate attributes.
func WithCorrelationID ¶ added in v0.2.0
WithCorrelationID attaches a correlation ID to the context. Correlation IDs are used to group related requests across services.
func WithRequestID ¶
WithRequestID attaches a request ID to the context.
Types ¶
type MiddlewareConfig ¶ added in v0.2.0
type MiddlewareConfig struct {
HeaderName string // request ID header (default "X-Request-ID")
GenerateID func() string // custom ID generator (default UUID v4)
LogRequests bool // log incoming requests
LogResponses bool // log completed responses
}
MiddlewareConfig configures the behavior of MiddlewareWithConfig.