-
Notifications
You must be signed in to change notification settings - Fork 3
/
logger.go
55 lines (45 loc) · 1.14 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
package main
import (
"os"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
const (
LogFile = "services/crawler/logs.ndjson"
)
type Logger struct {
*zap.Logger
}
type LogEntry struct {
Level string `json:"level"`
Time string `json:"time"`
Caller string `json:"caller"`
Message string `json:"message"`
Stacktrace string `json:"stacktrace"`
}
var EncoderConfig = zapcore.EncoderConfig{
TimeKey: "time",
LevelKey: "level",
NameKey: "logger",
CallerKey: "caller",
MessageKey: "message",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.CapitalLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}
func NewConsoleLogger() (*Logger, error) {
encoder := zapcore.NewJSONEncoder(EncoderConfig)
sync := zapcore.AddSync(os.Stdout)
fileLogger := zap.New(
zapcore.NewCore(encoder, sync, zapcore.DebugLevel),
zap.AddCaller(),
zap.AddStacktrace(zap.ErrorLevel),
zap.AddStacktrace(zap.WarnLevel),
)
return &Logger{
Logger: fileLogger,
}, nil
}