/
zaplog.go
79 lines (62 loc) · 1.58 KB
/
zaplog.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package common
import (
"fmt"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"os"
"strings"
)
var (
zapLog *zap.Logger
zapSugar *zap.SugaredLogger
logLevel = zap.NewAtomicLevelAt(zap.InfoLevel)
)
func init() {
var err error
var config zap.Config
encoderConfig := zap.NewDevelopmentEncoderConfig()
encoderConfig.StacktraceKey = "" // to hide stacktrace info
config.Level = logLevel
config.OutputPaths = []string{"./patrick.log"}
config.Development = true
config.DisableCaller = false
config.DisableStacktrace = false
config.Encoding = "console"
config.EncoderConfig = encoderConfig
if zapLog, err = config.Build(zap.AddCallerSkip(1)); err != nil {
panic(err)
}
zapSugar = zapLog.Sugar()
}
func LogLevel() zapcore.Level {
return zapSugar.Level()
}
func SetLogLevel(level string) {
var l zap.AtomicLevel
var err error
if l, err = zap.ParseAtomicLevel(strings.ToLower(level)); err != nil {
msg := fmt.Sprintf(ErrorTemplateParseError, err)
LogErrorf(msg)
fmt.Println(msg)
os.Exit(EXIT_CODE_CONFIGURATION_ERROR)
} else {
LogDebugf(LogTemplateSettingLogLevel, l)
logLevel.SetLevel(l.Level())
LogDebugf(LogTemplateSetLogLevel, l)
}
}
func LogInfof(message string, fields ...any) {
zapSugar.Infof(message, fields...)
}
func LogDebugf(message string, fields ...any) {
zapSugar.Debugf(message, fields...)
}
func LogWarnf(message string, fields ...any) {
zapSugar.Warnf(message, fields...)
}
func LogErrorf(message string, fields ...any) {
zapSugar.Errorf(message, fields...)
}
func LogFatalf(message string, fields ...any) {
zapSugar.Fatalf(message, fields...)
}