-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.go
More file actions
26 lines (20 loc) · 802 Bytes
/
context.go
File metadata and controls
26 lines (20 loc) · 802 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Copyright The ActForGood Authors.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://github.com/actforgood/xlog/blob/main/LICENSE.
package xlog
import "context"
// loggerCtxKey is the context key where logger is stored.
type loggerCtxKey struct{}
// ContextWithLogger returns a new context enriched with logger.
func ContextWithLogger(ctx context.Context, logger Logger) context.Context {
return context.WithValue(ctx, loggerCtxKey{}, logger)
}
// LoggerFromContext returns the logger stored in the context,
// or a [NopLogger] if none is present in the context.
func LoggerFromContext(ctx context.Context) Logger {
if logger, found := ctx.Value(loggerCtxKey{}).(Logger); found {
return logger
}
return NopLogger{}
}