Repository navigation
Expand file tree
/
Copy pathcontext.go
More file actions
54 lines (49 loc) · 1.93 KB
/
Copy pathcontext.go
File metadata and controls
54 lines (49 loc) · 1.93 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package log
import "context"
// loggerKey is the context key a request-scoped logger is stored under. It is
// an unexported type, so nothing outside this package can collide with it or
// replace what is there.
type loggerKey struct{}
// WithContext returns a copy of ctx carrying l.
//
// It exists so that a logger prepared once for a unit of work - a request, a
// job, a message - travels with that work instead of being threaded through
// every function that might want to log. The alternative in practice is not
// threading it: log lines end up written against a package-level logger, with
// nothing tying them to the request that produced them, and joining them
// afterwards means matching on timestamps.
//
// A nil logger is stored as given; [FromContext] reports it as absent, so a
// caller who cleared it deliberately gets the fallback rather than a panic.
func WithContext(ctx context.Context, l *Logger) context.Context {
if ctx == nil {
ctx = context.Background()
}
return context.WithValue(ctx, loggerKey{}, l)
}
// FromContext returns the logger stored in ctx, or nil when there is none.
//
// Use [FromContextOr] where a logger is required; this form is for code that
// wants to know whether one was provided at all.
func FromContext(ctx context.Context) *Logger {
if ctx == nil {
return nil
}
l, _ := ctx.Value(loggerKey{}).(*Logger)
return l
}
// FromContextOr returns the logger stored in ctx, or fallback when there is
// none:
//
// log.FromContextOr(ctx, defaultLogger).Errorf("upstream refused: %v", err)
//
// This is the form to reach for at a call site. It never returns nil unless
// the fallback is nil, so a handler that may or may not run inside a prepared
// context does not need to branch, and a missing logger degrades to logging
// somewhere rather than to a panic or to silence.
func FromContextOr(ctx context.Context, fallback *Logger) *Logger {
if l := FromContext(ctx); l != nil {
return l
}
return fallback
}