-
Notifications
You must be signed in to change notification settings - Fork 0
/
zapLogger.go
56 lines (43 loc) · 1.26 KB
/
zapLogger.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
package logger
import (
"go-graphql-mongo-server/config"
"os"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var Log *zap.SugaredLogger
func Initialize() {
loggerEncoder := zapcore.EncoderConfig{
TimeKey: "time",
LevelKey: "level",
NameKey: "logger",
CallerKey: "caller",
FunctionKey: "func",
MessageKey: "msg",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.LowercaseLevelEncoder,
EncodeTime: zapcore.RFC3339TimeEncoder,
EncodeDuration: zapcore.StringDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
ConsoleSeparator: "|\t|",
}
var loggerCore zapcore.Core
if config.Store.ProductionMode {
// For production, output logs in JSON Format
loggerCore = zapcore.NewCore(
zapcore.NewJSONEncoder(loggerEncoder),
zapcore.AddSync(os.Stdout),
zapcore.InfoLevel,
)
} else {
// For local dev mode, output logs in Console Format
loggerEncoder.EncodeLevel = zapcore.CapitalColorLevelEncoder
loggerCore = zapcore.NewCore(
zapcore.NewConsoleEncoder(loggerEncoder),
zapcore.AddSync(os.Stdout),
zapcore.DebugLevel,
)
}
Log = zap.New(loggerCore, zap.AddCaller(), zap.AddStacktrace(zap.ErrorLevel)).Sugar()
}