This commit is contained in:
antanst
2025-10-10 15:20:45 +03:00
parent 3a5835fc42
commit d336bdffba
10 changed files with 494 additions and 374 deletions

View File

@@ -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