forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 9
/
encoding.go
52 lines (45 loc) · 1.3 KB
/
encoding.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
package logp
import (
"go.uber.org/zap/zapcore"
)
var baseEncodingConfig = zapcore.EncoderConfig{
TimeKey: "timestamp",
LevelKey: "level",
NameKey: "logger",
CallerKey: "caller",
MessageKey: "message",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.LowercaseLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.NanosDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
EncodeName: zapcore.FullNameEncoder,
}
func buildEncoder(cfg Config) zapcore.Encoder {
if cfg.JSON {
return zapcore.NewJSONEncoder(jsonEncoderConfig())
} else if cfg.ToSyslog {
return zapcore.NewConsoleEncoder(syslogEncoderConfig())
} else {
return zapcore.NewConsoleEncoder(consoleEncoderConfig())
}
}
func jsonEncoderConfig() zapcore.EncoderConfig {
return baseEncodingConfig
}
func consoleEncoderConfig() zapcore.EncoderConfig {
c := baseEncodingConfig
c.EncodeLevel = zapcore.CapitalLevelEncoder
c.EncodeName = bracketedNameEncoder
return c
}
func syslogEncoderConfig() zapcore.EncoderConfig {
c := consoleEncoderConfig()
// Time is added by syslog.
c.TimeKey = ""
return c
}
func bracketedNameEncoder(loggerName string, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString("[" + loggerName + "]")
}