-
Notifications
You must be signed in to change notification settings - Fork 1
/
logging.go
129 lines (112 loc) · 3.09 KB
/
logging.go
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package logging
import (
"context"
"os"
"sync"
"time"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
type contextKey string
const loggerKey = contextKey("logger")
var (
defaultLogger *zap.SugaredLogger
defaultLoggerOnce sync.Once
)
const (
timestamp = "timestamp"
level = "level"
logger = "logger"
caller = "caller"
message = "message"
stacktrace = "stacktrace"
encodingConsole = "console"
encodingJSON = "json"
)
var outputStderr = []string{"stderr"}
var productionEncoderConfig = zapcore.EncoderConfig{
TimeKey: timestamp,
LevelKey: level,
NameKey: logger,
CallerKey: caller,
MessageKey: message,
StacktraceKey: stacktrace,
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: levelEncoder(),
EncodeTime: timeEncoder(),
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}
var developmentEncoderConfig = zapcore.EncoderConfig{
TimeKey: timestamp,
LevelKey: level,
NameKey: logger,
CallerKey: caller,
FunctionKey: zapcore.OmitKey,
MessageKey: message,
StacktraceKey: stacktrace,
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.CapitalLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.StringDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}
func DefaultLogger() *zap.SugaredLogger {
defaultLoggerOnce.Do(func() {
env := os.Getenv("ENVIRONMENT")
lv := os.Getenv("LOG_LEVEL")
defaultLogger = NewLogger(lv, env)
})
return defaultLogger
}
func NewLogger(l string, env string) *zap.SugaredLogger {
level := LogLevel(l)
var config *zap.Config
if env == "prod" {
config = &zap.Config{
Level: zap.NewAtomicLevelAt(levelToZapLevel(level)),
Encoding: encodingJSON,
EncoderConfig: productionEncoderConfig,
OutputPaths: outputStderr,
ErrorOutputPaths: outputStderr,
}
} else if env == "qc" {
config = &zap.Config{
Level: zap.NewAtomicLevelAt(levelToZapLevel(level)),
Development: true,
Encoding: encodingJSON,
EncoderConfig: developmentEncoderConfig,
OutputPaths: outputStderr,
ErrorOutputPaths: outputStderr,
}
} else {
config = &zap.Config{
Level: zap.NewAtomicLevelAt(levelToZapLevel(level)),
Development: true,
Encoding: encodingConsole,
EncoderConfig: developmentEncoderConfig,
OutputPaths: outputStderr,
ErrorOutputPaths: outputStderr,
}
}
logger, err := config.Build()
if err != nil {
logger = zap.NewNop()
}
return logger.Sugar()
}
func WithLogger(ctx context.Context, logger *zap.SugaredLogger) context.Context {
return context.WithValue(ctx, loggerKey, logger)
}
func FromContext(ctx context.Context) *zap.SugaredLogger {
if logger, ok := ctx.Value(loggerKey).(*zap.SugaredLogger); ok {
return logger
}
return DefaultLogger()
}
// timeEncoder encodes the time as RFC3339 nano.
func timeEncoder() zapcore.TimeEncoder {
return func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(t.Format(time.RFC3339Nano))
}
}