searcherside/internal/logging/context.go

21 lines
395 B
Go
Raw Normal View History

2024-06-06 20:08:51 +00:00
package logging
import (
"context"
"log/slog"
)
var loggerKey struct{}
func ContextWithLogger(ctx context.Context, logger *slog.Logger) context.Context {
return context.WithValue(ctx, loggerKey, logger)
}
func GetLogger(ctx context.Context) *slog.Logger {
contextLogger := ctx.Value(loggerKey).(*slog.Logger)
if contextLogger == nil {
return slog.Default()
}
return contextLogger
}