fix tests with logging and context
This commit is contained in:
@@ -10,17 +10,17 @@ type ContextHandler struct {
|
||||
slog.Handler
|
||||
}
|
||||
|
||||
func (h *ContextHandler) Handle(ctx context.Context, r slog.Record) error {
|
||||
func (h *ContextHandler) Handle(ctx context.Context, r slog.Record) error {
|
||||
if requestID, ok := ctx.Value("trace_id").(string); ok {
|
||||
r.AddAttrs(slog.String("trace_id", requestID))
|
||||
}
|
||||
return h.Handler.Handle(ctx, r)
|
||||
r.AddAttrs(slog.String("trace_id", requestID))
|
||||
}
|
||||
return h.Handler.Handle(ctx, r)
|
||||
}
|
||||
|
||||
func NewLogger() *slog.Logger {
|
||||
baseHandler := slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{AddSource: false})
|
||||
customHandler := &ContextHandler{Handler: baseHandler}
|
||||
logger := slog.New(customHandler)
|
||||
baseHandler := slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{AddSource: false})
|
||||
customHandler := &ContextHandler{Handler: baseHandler}
|
||||
logger := slog.New(customHandler)
|
||||
|
||||
return logger
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user