-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-log.go
44 lines (38 loc) · 953 Bytes
/
test-log.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
package main
import (
"encoding/json"
"time"
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func main() {
// initialize the rotator
logFile := "./app-%Y-%m-%d-%H.log"
rotator, err := rotatelogs.New(
logFile,
rotatelogs.WithMaxAge(60*24*time.Hour),
rotatelogs.WithRotationTime(time.Hour))
if err != nil {
panic(err)
}
// initialize the JSON encoding config
encoderConfig := map[string]string{
"levelEncoder": "capital",
"timeKey": "date",
"timeEncoder": "iso8601",
}
data, _ := json.Marshal(encoderConfig)
var encCfg zapcore.EncoderConfig
if err := json.Unmarshal(data, &encCfg); err != nil {
panic(err)
}
// add the encoder config and rotator to create a new zap logger
w := zapcore.AddSync(rotator)
core := zapcore.NewCore(
zapcore.NewJSONEncoder(encCfg),
w,
zap.InfoLevel)
logger := zap.New(core)
logger.Info("Now logging in a rotated file")
}