-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmsg.go
More file actions
191 lines (156 loc) · 3.03 KB
/
msg.go
File metadata and controls
191 lines (156 loc) · 3.03 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package log
import (
"fmt"
"strings"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
type V map[string]any
func formatArgs(args []any) string {
sb := strings.Builder{}
for i, v := range args {
sb.WriteString(fmt.Sprintf("%+v", v))
if len(args)-1 > i {
sb.WriteByte(' ')
}
}
return sb.String()
}
func Log(level zapcore.Level, v []any) {
if log == nil {
return
}
args := formatArgs(v)
switch level {
case zap.DebugLevel:
log.Debug(args)
case zap.InfoLevel:
log.Info(args)
case zap.WarnLevel:
log.Warn(args)
case zap.ErrorLevel:
log.Error(args)
case zap.DPanicLevel:
log.DPanic(args)
case zap.PanicLevel:
log.Panic(args)
case zap.FatalLevel:
log.Fatal(args)
}
}
func Logf(level zapcore.Level, format string, v []any) {
if log == nil {
return
}
switch level {
case zap.DebugLevel:
log.Debugf(format, v...)
case zap.InfoLevel:
log.Infof(format, v...)
case zap.WarnLevel:
log.Warnf(format, v...)
case zap.ErrorLevel:
log.Errorf(format, v...)
case zap.DPanicLevel:
log.DPanicf(format, v...)
case zap.PanicLevel:
log.Panicf(format, v...)
case zap.FatalLevel:
log.Fatalf(format, v...)
}
}
func Logw(level zapcore.Level, msg string, vars V) {
if log == nil {
return
}
var args []any
for k, v := range vars {
args = append(args, k, v)
}
switch level {
case zap.DebugLevel:
log.Debugw(msg, args...)
case zap.InfoLevel:
log.Infow(msg, args...)
case zap.WarnLevel:
log.Warnw(msg, args...)
case zap.ErrorLevel:
log.Errorw(msg, args...)
case zap.DPanicLevel:
log.DPanicw(msg, args...)
case zap.PanicLevel:
log.Panicw(msg, args...)
case zap.FatalLevel:
log.Fatalw(msg, args...)
}
}
// DEBUG
func Debug(v ...any) {
Log(zap.DebugLevel, v)
}
func Debugf(format string, v ...any) {
Logf(zap.DebugLevel, format, v)
}
func Debugw(msg string, vars V) {
Logw(zap.DebugLevel, msg, vars)
}
// INFO
func Info(v ...any) {
Log(zap.InfoLevel, v)
}
func Infof(format string, v ...any) {
Logf(zap.InfoLevel, format, v)
}
func Infow(msg string, vars V) {
Logw(zap.InfoLevel, msg, vars)
}
// WARN
func Warn(v ...any) {
Log(zap.WarnLevel, v)
}
func Warnf(format string, v ...any) {
Logf(zap.WarnLevel, format, v)
}
func Warnw(msg string, vars V) {
Logw(zap.WarnLevel, msg, vars)
}
// ERROR
func Error(v ...any) {
Log(zap.ErrorLevel, v)
}
func Errorf(format string, v ...any) {
Logf(zap.ErrorLevel, format, v)
}
func Errorw(msg string, vars V) {
Logw(zap.ErrorLevel, msg, vars)
}
// DPANIC
func DPanic(v ...any) {
Log(zap.DPanicLevel, v)
}
func DPanicf(format string, v ...any) {
Logf(zap.DPanicLevel, format, v)
}
func DPanicw(msg string, vars V) {
Logw(zap.DPanicLevel, msg, vars)
}
// PANIC
func Panic(v ...any) {
Log(zap.PanicLevel, v)
}
func Panicf(format string, v ...any) {
Logf(zap.PanicLevel, format, v)
}
func Panicw(msg string, vars V) {
Logw(zap.PanicLevel, msg, vars)
}
// FATAL
func Fatal(v ...any) {
Log(zap.FatalLevel, v)
}
func Fatalf(format string, v ...any) {
Logf(zap.FatalLevel, format, v)
}
func Fatalw(msg string, vars V) {
Logw(zap.FatalLevel, msg, vars)
}