-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzl.go
303 lines (272 loc) Β· 7.03 KB
/
zl.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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// Package zl is a logger based on zap.
// It provides advanced logging features.
// It is designed with the developer's experience in mind and allows
// the user to choose the best output format for their purposes.
package zl
import (
"fmt"
"io"
"log"
"os"
"os/exec"
"os/signal"
"strings"
"sync"
"syscall"
"github.com/samber/lo"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
const (
consoleFieldDefault = "console"
)
var (
once sync.Once
pretty *prettyLogger
zapLogger *zap.Logger
encoderConfig *zapcore.EncoderConfig
internalLogger *zap.Logger
outputType Output
version string
severityLevel zapcore.Level // Default is InfoLevel
callerEncoder zapcore.CallerEncoder
consoleFields = []string{consoleFieldDefault}
omitKeys []Key
fieldKeys = make(map[Key]string)
isStdOut bool
separator = " "
pid int
isTest bool
)
type fatalHook struct{}
func (f fatalHook) OnWrite(_ *zapcore.CheckedEntry, _ []zapcore.Field) {
pretty.showErrorReport(fileName, pid)
if isTest {
fmt.Println("os.Exit(1) called.")
} else {
os.Exit(1)
}
}
// Init initializes the logger.
func Init() {
once.Do(func() {
encoderConfig = newEncoderConfig()
zapLogger = newLogger(encoderConfig)
if outputType == PrettyOutput || isTest {
pretty = newPrettyLogger(getConsoleOutput(), os.Stderr)
zapLogger = zapLogger.WithOptions(zap.WithFatalHook(fatalHook{}))
}
encInternal := newEncoderConfig()
encInternal.EncodeCaller = zapcore.ShortCallerEncoder
internalLogger = newLogger(encInternal)
var p, f string
if pid != 0 {
p = fmt.Sprintf(", PID: %d", pid)
}
if outputType == PrettyOutput || outputType == ConsoleAndFileOutput {
f = fmt.Sprintf(", File: %s", fileName)
}
c := fmt.Sprintf(
"Severity: %s, Output: %s%s%s",
severityLevel.CapitalString(),
outputType.String(),
f,
p,
)
iDebug("INIT_LOGGER", Console(c))
})
}
func newEncoderConfig() *zapcore.EncoderConfig {
enc := zapcore.EncoderConfig{
MessageKey: fieldKey(MessageKey),
LevelKey: fieldKey(LevelKey),
TimeKey: fieldKey(TimeKey),
NameKey: fieldKey(LoggerKey),
CallerKey: fieldKey(CallerKey),
FunctionKey: fieldKey(FunctionKey),
StacktraceKey: fieldKey(StacktraceKey),
EncodeLevel: zapcore.CapitalLevelEncoder,
EncodeTime: zapcore.RFC3339NanoTimeEncoder,
EncodeDuration: zapcore.StringDurationEncoder,
EncodeCaller: getCallerEncoder(),
}
setOmitKeys(&enc)
return &enc
}
func fieldKey(key Key) string {
value, ok := fieldKeys[key]
if !ok {
return string(key)
}
return value
}
// See https://pkg.go.dev/go.uber.org/zap
func newLogger(enc *zapcore.EncoderConfig) *zap.Logger {
core := zapcore.NewCore(
zapcore.NewJSONEncoder(*enc),
zapcore.NewMultiWriteSyncer(getSyncers()...),
severityLevel,
)
return zap.New(core,
zap.AddCallerSkip(1),
zap.AddCaller(),
zap.AddStacktrace(zapcore.ErrorLevel),
).With(getAdditionalFields()...)
}
func setOmitKeys(enc *zapcore.EncoderConfig) {
for i := range omitKeys {
switch omitKeys[i] {
case MessageKey:
enc.MessageKey = zapcore.OmitKey
case LevelKey:
enc.LevelKey = zapcore.OmitKey
case TimeKey:
enc.TimeKey = zapcore.OmitKey
case LoggerKey:
enc.NameKey = zapcore.OmitKey
case CallerKey:
enc.CallerKey = zapcore.OmitKey
case FunctionKey:
enc.FunctionKey = zapcore.OmitKey
case StacktraceKey:
enc.StacktraceKey = zapcore.OmitKey
}
}
}
func getAdditionalFields() (fields []zapcore.Field) {
if !lo.Contains(omitKeys, VersionKey) {
fields = append(fields, zap.String(string(VersionKey), GetVersion()))
}
if !lo.Contains(omitKeys, HostnameKey) {
fields = append(fields, zap.String(string(HostnameKey), *getHost()))
}
if !lo.Contains(omitKeys, PIDKey) {
pid = os.Getpid()
fields = append(fields, zap.Int(string(PIDKey), pid))
}
return fields
}
// GetVersion return version when version is set.
// or return git commit hash when version is not set.
func GetVersion() string {
if version != "" {
return version
}
if out, err := exec.Command("git", "rev-parse", "--short", "HEAD").Output(); err == nil {
return strings.TrimRight(string(out), "\n")
}
return "undefined"
}
// Sync is wrapper of Zap's Sync.
//
// Flushes any buffered log entries.(See: https://pkg.go.dev/go.uber.org/zap#Logger.Sync)
// Applications should take care to call Sync before exiting.
//
// Also displays an error report with a formatted stack trace if the outputType is PrettyOutput.
// This is useful for finding the source of errors during development.
//
// An error will occur if zap's Sync is executed when the output destination is console.
// (See: https://github.com/uber-go/zap/issues/880 )
// Therefore, Sync is executed only when console is not included in the zap output destination.
func Sync() {
if outputType != PrettyOutput && outputType != FileOutput {
return
}
if err := zapLogger.Sync(); err != nil {
log.Println(err)
}
if outputType == PrettyOutput {
pretty.showErrorReport(fileName, pid)
}
}
// SyncWhenStop flush log buffer. when interrupt or terminated.
func SyncWhenStop() {
if outputType != PrettyOutput && outputType != FileOutput {
return
}
c := make(chan os.Signal, 1)
go func() {
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
s := <-c
sigCode := 0
switch s.String() {
case "interrupt":
sigCode = 2
case "terminated":
sigCode = 15
}
iDebug(fmt.Sprintf("GOT_SIGNAL_%v", strings.ToUpper(s.String())))
Sync() // flush log buffer
if isTest {
fmt.Printf("os.Exit(%d) called.", 128+sigCode)
} else {
os.Exit(128 + sigCode)
}
}()
}
func getHost() *string {
ret, err := os.Hostname()
if err != nil {
log.Print(err)
return nil
}
return &ret
}
func getCallerEncoder() zapcore.CallerEncoder {
if callerEncoder != nil {
return callerEncoder
}
return zapcore.ShortCallerEncoder
}
func getSyncers() (syncers []zapcore.WriteSyncer) {
switch outputType {
case PrettyOutput, FileOutput:
syncers = append(syncers, zapcore.AddSync(newRotator()))
case ConsoleAndFileOutput:
syncers = append(syncers, zapcore.AddSync(getConsoleOutput()), zapcore.AddSync(newRotator()))
case ConsoleOutput:
syncers = append(syncers, zapcore.AddSync(getConsoleOutput()))
}
return
}
func getConsoleOutput() io.Writer {
if isStdOut {
return os.Stdout
} else {
return os.Stderr
}
}
// ResetGlobalLoggerSettings resets global logger settings.
// This is convenient for use in tests, etc.
func ResetGlobalLoggerSettings() {
once = sync.Once{}
pretty = nil
zapLogger = nil
encoderConfig = nil
internalLogger = nil
outputType = PrettyOutput
version = ""
severityLevel = zapcore.InfoLevel
callerEncoder = nil
consoleFields = []string{consoleFieldDefault}
omitKeys = nil
fieldKeys = make(map[Key]string)
isStdOut = false
separator = " "
fileName = ""
maxSize = 0
maxBackups = 0
maxAge = 0
localTime = false
compress = false
}
// Cleanup
// Deprecated: Use ResetGlobalLoggerSettings instead.
// # codecov ignore
func Cleanup() {
ResetGlobalLoggerSettings()
}
// SetIsTest sets isTest flag to true.
func SetIsTest() {
isTest = true
}