-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.logger.go
51 lines (40 loc) · 1000 Bytes
/
common.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
package main
import (
"log/slog"
"go.uber.org/zap"
"go.uber.org/zap/exp/zapslog"
"go.uber.org/zap/zapcore"
)
var (
logger *zap.SugaredLogger
slogger *slog.Logger
)
func initLogger() *zap.SugaredLogger {
var config zap.Config
if Opts.Logger.Development {
config = zap.NewDevelopmentConfig()
config.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
} else {
config = zap.NewProductionConfig()
}
config.Encoding = "console"
config.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
// debug level
if Opts.Logger.Debug {
config.Level = zap.NewAtomicLevelAt(zapcore.DebugLevel)
}
// json log format
if Opts.Logger.Json {
config.Encoding = "json"
// if running in containers, logs already enriched with timestamp by the container runtime
config.EncoderConfig.TimeKey = ""
}
// build logger
log, err := config.Build()
if err != nil {
panic(err)
}
logger = log.Sugar()
slogger = slog.New(zapslog.NewHandler(log.Core(), nil))
return logger
}