mirror of
https://git.numenor-labs.us/dsfx.git
synced 2025-04-29 08:10:34 +00:00
25 lines
460 B
Go
25 lines
460 B
Go
package logging
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
)
|
|
|
|
type contextKey int
|
|
|
|
var ctxKey contextKey = 1
|
|
|
|
// FromContext retrieves the slog.Logger from the context.
|
|
func FromContext(ctx context.Context) *slog.Logger {
|
|
logger, ok := ctx.Value(ctxKey).(*slog.Logger)
|
|
if !ok {
|
|
return slog.Default()
|
|
}
|
|
return logger
|
|
}
|
|
|
|
// WithContext ...
|
|
func WithContext(ctx context.Context, logger *slog.Logger) context.Context {
|
|
return context.WithValue(ctx, ctxKey, logger)
|
|
}
|