Redmage/log/context.go

22 lines
413 B
Go
Raw Normal View History

2024-04-06 01:22:00 +07:00
package log
import (
"context"
"log/slog"
)
type loggerKey struct{}
func FromContext(ctx context.Context) slog.Handler {
h, _ := ctx.Value(loggerKey{}).(slog.Handler)
return h
}
func WithContext(ctx context.Context, l slog.Handler) context.Context {
return context.WithValue(ctx, loggerKey{}, l)
}
func NullHandlerContext(ctx context.Context) context.Context {
return WithContext(ctx, NullHandler{})
}