Pool managers will have 2 fields identifying which manager generated the log line. In the future, we will add tracking ids in various cases, allowing us to track down issues faster. Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
30 lines
540 B
Go
30 lines
540 B
Go
package util
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
)
|
|
|
|
type slogContextKey string
|
|
|
|
const (
|
|
slogCtxFields slogContextKey = "slog_ctx_fields"
|
|
)
|
|
|
|
type ContextHandler struct {
|
|
slog.Handler
|
|
}
|
|
|
|
func (h ContextHandler) Handle(ctx context.Context, r slog.Record) error {
|
|
attrs, ok := ctx.Value(slogCtxFields).([]slog.Attr)
|
|
if ok {
|
|
for _, v := range attrs {
|
|
r.AddAttrs(v)
|
|
}
|
|
}
|
|
return h.Handler.Handle(ctx, r)
|
|
}
|
|
|
|
func WithContext(ctx context.Context, attrs ...slog.Attr) context.Context {
|
|
return context.WithValue(ctx, slogCtxFields, attrs)
|
|
}
|