This repository has been archived by the owner on Apr 5, 2024. It is now read-only.
/
logger.go
66 lines (61 loc) · 1.63 KB
/
logger.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
package main
import (
"strings"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// NewStackdriver creates a new zap logger for stackdriver
func NewStackdriver(level string) (*zap.Logger, error) {
return zap.Config{
Level: zap.NewAtomicLevelAt(logLevel(level)),
Development: false,
Sampling: &zap.SamplingConfig{
Initial: 100,
Thereafter: 100,
},
Encoding: "json",
EncoderConfig: zapcore.EncoderConfig{
LevelKey: "severity",
NameKey: "logger",
CallerKey: "caller",
StacktraceKey: "stack_trace",
TimeKey: "time",
MessageKey: "message",
LineEnding: zapcore.DefaultLineEnding,
EncodeTime: zapcore.RFC3339NanoTimeEncoder,
EncodeLevel: levelEncode,
EncodeCaller: zapcore.ShortCallerEncoder,
},
DisableStacktrace: true,
OutputPaths: []string{"stdout"},
ErrorOutputPaths: []string{"stderr"},
}.Build()
}
func logLevel(level string) zapcore.Level {
level = strings.ToUpper(level)
switch level {
case "DEBUG":
return zapcore.DebugLevel
case "WARN":
return zapcore.WarnLevel
case "ERROR":
return zapcore.ErrorLevel
}
return zapcore.InfoLevel
}
// severityName is copied from these lines.
// https://github.com/googleapis/google-cloud-go/blob/6637d0c93932ed839229391d7796785cc4d7cc21/logging/logging.go#L440-L450
func levelEncode(l zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
switch l {
case zapcore.DebugLevel:
enc.AppendString("Debug")
case zapcore.InfoLevel:
enc.AppendString("Info")
case zapcore.WarnLevel:
enc.AppendString("Warning")
case zapcore.ErrorLevel:
enc.AppendString("Error")
default:
enc.AppendString("Critical")
}
}