-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmain.go
55 lines (39 loc) · 1.26 KB
/
main.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
package main
import (
"fmt"
"time"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func SyslogTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(t.Format("Jan 2 15:04:05"))
}
func CustomLevelEncoder(level zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString("[" + level.CapitalString() + "]")
}
func main() {
cfg := zap.Config{
Encoding: "console",
OutputPaths: []string{"stderr"},
EncoderConfig: zapcore.EncoderConfig{
MessageKey: "message",
TimeKey: "time",
EncodeTime: zapcore.ISO8601TimeEncoder,
LevelKey: "level",
EncodeLevel: zapcore.CapitalLevelEncoder,
},
}
fmt.Printf("\n*** Using standard ISO8601 time encoder\n\n")
// avoiding copying of atomic values
cfg.Level = zap.NewAtomicLevelAt(zapcore.DebugLevel)
logger, _ := cfg.Build()
logger.Info("This should have an ISO8601 based time stamp")
fmt.Printf("\n*** Using a custom time encoder\n\n")
cfg.EncoderConfig.EncodeTime = SyslogTimeEncoder
logger, _ = cfg.Build()
logger.Info("This should have a syslog style time stamp")
fmt.Printf("\n*** Using a custom level encoder\n\n")
cfg.EncoderConfig.EncodeLevel = CustomLevelEncoder
logger, _ = cfg.Build()
logger.Info("This should have a interesting level name")
}