.
This commit is contained in:
@@ -11,9 +11,9 @@ import (
|
||||
"github.com/lmittmann/tint"
|
||||
)
|
||||
|
||||
type contextKey int
|
||||
type contextKey string
|
||||
|
||||
const loggerKey contextKey = 0
|
||||
const CtxLoggerKey contextKey = "logger"
|
||||
|
||||
var (
|
||||
programLevel *slog.LevelVar = new(slog.LevelVar) // Info by default
|
||||
@@ -21,11 +21,15 @@ var (
|
||||
)
|
||||
|
||||
func WithLogger(ctx context.Context, logger *slog.Logger) context.Context {
|
||||
return context.WithValue(ctx, loggerKey, logger)
|
||||
return context.WithValue(ctx, CtxLoggerKey, logger)
|
||||
}
|
||||
|
||||
func WithAttr(logger *slog.Logger, attrName string, attrValue interface{}) *slog.Logger {
|
||||
return logger.With(attrName, attrValue)
|
||||
}
|
||||
|
||||
func FromContext(ctx context.Context) *slog.Logger {
|
||||
if logger, ok := ctx.Value(loggerKey).(*slog.Logger); ok {
|
||||
if logger, ok := ctx.Value(CtxLoggerKey).(*slog.Logger); ok {
|
||||
return logger
|
||||
}
|
||||
// Return default logger instead of panicking
|
||||
|
||||
Reference in New Issue
Block a user