feat: base implementation
This commit is contained in:
23
core/services/jlog/context.go
Normal file
23
core/services/jlog/context.go
Normal file
@ -0,0 +1,23 @@
|
||||
package jlog
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
)
|
||||
|
||||
type ctxKey string
|
||||
|
||||
const ctxkeyLogger ctxKey = "jlog:logger"
|
||||
|
||||
func FromContext(ctx context.Context) *slog.Logger {
|
||||
logger, exists := ctx.Value(ctxkeyLogger).(*slog.Logger)
|
||||
if !exists {
|
||||
return New(slog.LevelInfo)
|
||||
}
|
||||
|
||||
return logger
|
||||
}
|
||||
|
||||
func ContextWith(ctx context.Context, logger *slog.Logger) context.Context {
|
||||
return context.WithValue(ctx, ctxkeyLogger, logger)
|
||||
}
|
||||
Reference in New Issue
Block a user