-
Notifications
You must be signed in to change notification settings - Fork 0
/
zap.go
114 lines (99 loc) · 2.91 KB
/
zap.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
package logger
import (
"os"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// Zap is a logger implementation using zap.
type Zap struct {
logger *zap.Logger
Config Config
}
// NewZap returns a new *Zap.
func NewZap(config Config) *Zap {
atomicLevel := zap.NewAtomicLevel()
switch config.Level {
case DebugLevel:
atomicLevel.SetLevel(zap.DebugLevel)
case InfoLevel:
atomicLevel.SetLevel(zap.InfoLevel)
case WarnLevel:
atomicLevel.SetLevel(zap.WarnLevel)
case ErrorLevel:
atomicLevel.SetLevel(zap.ErrorLevel)
case FatalLevel:
atomicLevel.SetLevel(zap.FatalLevel)
default:
atomicLevel.SetLevel(zap.InfoLevel)
}
output := zapcore.AddSync(config.Output)
encoderConfig := zap.NewProductionEncoderConfig()
encoderConfig.CallerKey = "caller"
encoderConfig.EncodeCaller = zapcore.FullCallerEncoder
encoderConfig.EncodeTime = zapcore.RFC3339TimeEncoder
core := zapcore.NewCore(zapcore.NewJSONEncoder(encoderConfig), output, atomicLevel)
logger := zap.New(core, zap.AddCaller(), zap.AddCallerSkip(1))
if config.ExitFunc == nil {
config.ExitFunc = os.Exit // default to os.Exit
}
return &Zap{
logger: logger,
Config: config,
}
}
// Debug logs a debug message with structured fields.
func (z *Zap) Debug(msg string, fields Fields) {
if z.shouldLog(DebugLevel) {
z.logger.Debug(msg, mapToZapFields(fields)...)
}
}
// Info logs an info message with structured fields.
func (z *Zap) Info(msg string, fields Fields) {
if z.shouldLog(InfoLevel) {
z.logger.Info(msg, mapToZapFields(fields)...)
}
}
// Warn logs a warning message with structured fields.
func (z *Zap) Warn(msg string, fields Fields) {
if z.shouldLog(WarnLevel) {
z.logger.Warn(msg, mapToZapFields(fields)...)
}
}
// Error logs an error message with structured fields.
func (z *Zap) Error(msg string, fields Fields) {
if z.shouldLog(ErrorLevel) {
z.logger.Error(msg, mapToZapFields(fields)...)
}
}
// Fatal logs a fatal message with structured fields and exits the application.
func (z *Zap) Fatal(msg string, fields Fields) {
if z.shouldLog(FatalLevel) {
z.logger.Fatal(msg, mapToZapFields(fields)...)
z.Config.ExitFunc(1)
}
}
// shouldLog determines if a log entry should be logged based on the log level.
func (z *Zap) shouldLog(level Level) bool {
return level >= z.Config.Level
}
// mapToZapFields converts Fields to zap.Field with type-specific handling for better performance.
func mapToZapFields(fields Fields) []zap.Field {
zapFields := make([]zap.Field, 0, len(fields))
for k, v := range fields {
switch val := v.(type) {
case string:
zapFields = append(zapFields, zap.String(k, val))
case int:
zapFields = append(zapFields, zap.Int(k, val))
case int64:
zapFields = append(zapFields, zap.Int64(k, val))
case float64:
zapFields = append(zapFields, zap.Float64(k, val))
case bool:
zapFields = append(zapFields, zap.Bool(k, val))
default:
zapFields = append(zapFields, zap.Any(k, v))
}
}
return zapFields
}