forked from JamesLaverack/kubernetes-minecraft-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogutil.go
More file actions
30 lines (25 loc) · 784 Bytes
/
logutil.go
File metadata and controls
30 lines (25 loc) · 784 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
27
28
29
30
package logutil
import (
"context"
"go.uber.org/zap"
)
const LoggerKey = "ZapLogger"
// FromContextOrNew will either pull an existing *zap.Logger from the context, or create a new one. It doesn't then add
// that new logger to the context, that's left as a task for the caller.
func FromContextOrNew(ctx context.Context) *zap.Logger {
l := ctx.Value(LoggerKey)
log, ok := l.(*zap.Logger)
if !ok {
log, err := zap.NewProduction()
if err != nil {
panic(err)
}
return log
}
return log
}
// IntoContext returns a new context based on ctx with the provided *zap.Logger added. This can be later retrieved using
// the FromContextOrNew function.
func IntoContext(ctx context.Context, log *zap.Logger) context.Context {
return context.WithValue(ctx, LoggerKey, log)
}