Append pool_type and pool_mgr info to logs

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>
This commit is contained in:
Gabriel Adrian Samfira 2024-01-06 00:21:50 +00:00
parent e441b6ce89
commit 61e97f0896
6 changed files with 39 additions and 12 deletions

30
util/logging.go Normal file
View file

@ -0,0 +1,30 @@
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)
}