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
80
81
|
package logger
import (
"fmt"
"os"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var (
instance *zap.Logger
atomicLevel zap.AtomicLevel
)
func init() {
atomicLevel = zap.NewAtomicLevelAt(zapcore.InfoLevel)
encoderConfig := zapcore.EncoderConfig{
LevelKey: "level",
MessageKey: "msg",
LineEnding: "\n",
EncodeLevel: formatLevel,
}
encoder := zapcore.NewConsoleEncoder(encoderConfig)
stdoutSink := zapcore.AddSync(os.Stdout)
stderrSink := zapcore.AddSync(os.Stderr)
core := zapcore.NewTee(
zapcore.NewCore(encoder, stdoutSink, zap.LevelEnablerFunc(func(level zapcore.Level) bool {
return level < zapcore.WarnLevel && atomicLevel.Enabled(level)
})),
zapcore.NewCore(encoder, stderrSink, zap.LevelEnablerFunc(func(level zapcore.Level) bool {
return level >= zapcore.WarnLevel && atomicLevel.Enabled(level)
})),
)
instance = zap.New(core, zap.AddCaller())
}
func SetDebug(enabled bool) {
if enabled {
atomicLevel.SetLevel(zapcore.DebugLevel)
} else {
atomicLevel.SetLevel(zapcore.InfoLevel)
}
}
func Debugf(prefix string, format string, arguments ...any) {
emit(LevelDebug, zapcore.DebugLevel, prefix, fmt.Sprintf(format, arguments...))
}
func Infof(prefix string, format string, arguments ...any) {
emit(LevelInfo, zapcore.InfoLevel, prefix, fmt.Sprintf(format, arguments...))
}
func Successf(prefix string, format string, arguments ...any) {
emit(LevelSuccess, zapcore.InfoLevel, prefix, fmt.Sprintf(format, arguments...))
}
func Warnf(prefix string, format string, arguments ...any) {
emit(LevelWarn, zapcore.WarnLevel, prefix, fmt.Sprintf(format, arguments...))
}
func Errorf(prefix string, format string, arguments ...any) {
emit(LevelError, zapcore.ErrorLevel, prefix, fmt.Sprintf(format, arguments...))
}
func Fatalf(prefix string, format string, arguments ...any) {
emit(LevelError, zapcore.ErrorLevel, prefix, fmt.Sprintf(format, arguments...))
os.Exit(1)
}
func emit(levelLabel LogLevel, zapLevel zapcore.Level, prefix string, message any) {
if instance == nil {
panic(NotInitialized)
}
instance.Log(zapLevel, buildFullMessage(levelLabel, prefix, message))
}
|