Documentation
¶
Overview ¶
Package clog provides contextual logging compatible with log/slog.
It combines two mechanisms:
Context Handler — wrap any slog.Handler with NewContextHandler(inner, keys). For each log record, values for the given context keys are read from context.Context and added as attributes. Use context.WithValue to set values (e.g. trace_id, user_id) and pass the same context to logging calls.
Logger in context — store a *slog.Logger in the context with NewContext, retrieve it with FromContext. In middleware, create a request-scoped logger (e.g. With("request_id", id)) and attach it to the context so handlers can call Info(ctx, "message") without passing attributes every time.
Example setup:
h := clog.NewContextHandler(slog.NewJSONHandler(os.Stdout, nil),
[]clog.ContextKey{"trace_id", "user_id"})
slog.SetDefault(slog.New(h))
Example in HTTP middleware:
ctx = clog.NewContext(ctx, clog.FromContext(ctx).With("request_id", id, "path", r.URL.Path))
Example in a handler:
clog.Info(ctx, "request started") clog.Error(ctx, "operation failed", "error", err)
Index ¶
- func Debug(ctx context.Context, msg string, args ...any)
- func Error(ctx context.Context, msg string, args ...any)
- func ErrorLevel(ctx context.Context, err error, level slog.Level)
- func Errorf(ctx context.Context, msg string, args ...any)
- func FromContext(ctx context.Context) *slog.Logger
- func Info(ctx context.Context, msg string, args ...any)
- func Log(ctx context.Context, level slog.Level, msg string, args ...any)
- func LogAttrs(ctx context.Context, level slog.Level, msg string, attrs ...slog.Attr)
- func NewContext(ctx context.Context, logger *slog.Logger) context.Context
- func Warn(ctx context.Context, msg string, args ...any)
- func With(ctx context.Context, args ...any) context.Context
- func WithGroup(ctx context.Context, name string) context.Context
- type ContextHandler
- type ContextKey
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ErrorLevel ¶
ErrorLevel logs err at the given slog level using the logger from ctx (or the default). The error is logged with all structured attributes and stack trace from the muonsoft/errors chain. If err is nil, nothing is logged.
func Errorf ¶
Errorf creates an error with errors.Errorf(msg, args...) and logs it at Error level using the logger from ctx (or the default). The error is logged with all structured attributes and stack trace from the muonsoft/errors chain.
func FromContext ¶
FromContext returns the Logger stored in ctx, or slog.Default() if none. Use this to obtain the request-scoped logger when one was set via NewContext.
func LogAttrs ¶
LogAttrs logs at the given level with the given attrs using the logger from ctx (or the default). It is more efficient than Log when all arguments are already Attrs.
func NewContext ¶
NewContext returns a copy of ctx that stores the given Logger. Retrieve it later with FromContext. Use this in middleware to attach a request-scoped logger (e.g. with request_id, path) to the context.
Types ¶
type ContextHandler ¶
type ContextHandler struct {
// contains filtered or unexported fields
}
ContextHandler wraps a slog.Handler and adds attributes from context.Context for each key in keys. Values are retrieved via ctx.Value(key) and added to the record before passing to the inner handler.
func NewContextHandler ¶
func NewContextHandler(inner slog.Handler, keys []ContextKey) *ContextHandler
NewContextHandler returns a Handler that adds context attributes for the given keys to every record, then delegates to inner.
func (*ContextHandler) Enabled ¶
Enabled reports whether the handler handles records at the given level.
func (*ContextHandler) Handle ¶
Handle adds attributes from ctx for each configured key, then passes the record to the inner handler.
type ContextKey ¶
type ContextKey string
ContextKey is the type for context keys that ContextHandler reads to add attributes to each log record. Use it with context.WithValue.