mirror of
https://git.numenor-labs.us/dsfx.git
synced 2025-04-29 16:20:34 +00:00
25 lines
457 B
Go
25 lines
457 B
Go
|
package dlog
|
||
|
|
||
|
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)
|
||
|
}
|