-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
115 lines (104 loc) · 3.18 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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package zaplogger
import (
"fmt"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"os"
)
// Logger wraps the zap.Logger, providing additional configuration options.
type Logger struct {
*zap.Logger
conf *Config
}
// defaultLogger is the global instance of the Logger.
var defaultLogger *Logger
// Init initializes the Logger with the provided configuration.
// If the configuration is nil or incomplete, default values are applied.
func Init(conf *Config) error {
if conf == nil {
return fmt.Errorf("logger configuration is nil, please check it")
}
if conf.Level == "" {
conf.Level = "debug"
}
level, err := zapcore.ParseLevel(conf.Level)
if err != nil {
return err
}
if conf.Format == "" {
conf.Format = "console"
}
if conf.ApplyEncoder == nil {
conf.ApplyEncoder = func(format string, ec zapcore.EncoderConfig, conf BasicConfig) (zapcore.Encoder, error) {
if format == "json" {
return zapcore.NewJSONEncoder(ec), nil
}
return zapcore.NewConsoleEncoder(ec), nil
}
}
if conf.ApplyCores == nil {
conf.ApplyCores = func(enc zapcore.Encoder, level zapcore.LevelEnabler, conf BasicConfig) (zapcore.Core, error) {
return zapcore.NewCore(enc, os.Stderr, level), nil
}
}
ec := zapcore.EncoderConfig{
TimeKey: "timestamp",
LevelKey: "level",
CallerKey: "caller",
MessageKey: "msg",
FunctionKey: zapcore.OmitKey,
EncodeTime: zapcore.TimeEncoderOfLayout("2006-01-02 15:04:05"),
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
EncodeLevel: zapcore.CapitalLevelEncoder,
LineEnding: zapcore.DefaultLineEnding,
StacktraceKey: "stacktrace",
ConsoleSeparator: conf.ConsoleSeparator,
}
if conf.LevelColor && conf.Format == "console" {
// Apply level color coding when in console format
ec.EncodeLevel = zapcore.CapitalColorLevelEncoder
}
if conf.PrintStacktrace {
conf.Options = append(conf.Options, zap.AddCaller(), zap.AddStacktrace(zapcore.ErrorLevel))
}
encoder, err := conf.ApplyEncoder(conf.Format, ec, conf.BasicConfig) // Apply encoder configuration
if err != nil {
return err
}
cores, err := conf.ApplyCores(encoder, level, conf.BasicConfig) // Apply core configuration
if err != nil {
return err
}
l := zap.New(cores, conf.Options...)
defaultLogger = &Logger{l, conf}
return nil
}
// DefaultLogger returns the global instance of the Logger.
func DefaultLogger() *Logger {
return defaultLogger
}
// Info logs an informational message.
func Info(msg string, fields ...zap.Field) {
defaultLogger.Info(msg, fields...)
}
// Debug logs a debug-level message.
func Debug(msg string, fields ...zap.Field) {
defaultLogger.Debug(msg, fields...)
}
// Warn logs a warning message.
func Warn(msg string, fields ...zap.Field) {
defaultLogger.Warn(msg, fields...)
}
// Error logs an error message.
func Error(msg string, fields ...zap.Field) {
defaultLogger.Error(msg, fields...)
}
// Fatal logs a fatal-level message and exits the application.
func Fatal(msg string, fields ...zap.Field) {
defaultLogger.Fatal(msg, fields...)
}
// Panic logs a panic-level message and panics.
func Panic(msg string, fields ...zap.Field) {
defaultLogger.Panic(msg, fields...)
}