@@ -68,22 +68,30 @@ func Close() error {
68
68
69
69
// Debug writes v into access log.
70
70
func Debug (v ... any ) {
71
- writeDebug (fmt .Sprint (v ... ))
71
+ if shallLog (DebugLevel ) {
72
+ writeDebug (fmt .Sprint (v ... ))
73
+ }
72
74
}
73
75
74
76
// Debugf writes v with format into access log.
75
77
func Debugf (format string , v ... any ) {
76
- writeDebug (fmt .Sprintf (format , v ... ))
78
+ if shallLog (DebugLevel ) {
79
+ writeDebug (fmt .Sprintf (format , v ... ))
80
+ }
77
81
}
78
82
79
83
// Debugv writes v into access log with json content.
80
84
func Debugv (v any ) {
81
- writeDebug (v )
85
+ if shallLog (DebugLevel ) {
86
+ writeDebug (v )
87
+ }
82
88
}
83
89
84
90
// Debugw writes msg along with fields into access log.
85
91
func Debugw (msg string , fields ... LogField ) {
86
- writeDebug (msg , fields ... )
92
+ if shallLog (DebugLevel ) {
93
+ writeDebug (msg , fields ... )
94
+ }
87
95
}
88
96
89
97
// Disable disables the logging.
@@ -99,35 +107,47 @@ func DisableStat() {
99
107
100
108
// Error writes v into error log.
101
109
func Error (v ... any ) {
102
- writeError (fmt .Sprint (v ... ))
110
+ if shallLog (ErrorLevel ) {
111
+ writeError (fmt .Sprint (v ... ))
112
+ }
103
113
}
104
114
105
115
// Errorf writes v with format into error log.
106
116
func Errorf (format string , v ... any ) {
107
- writeError (fmt .Errorf (format , v ... ).Error ())
117
+ if shallLog (ErrorLevel ) {
118
+ writeError (fmt .Errorf (format , v ... ).Error ())
119
+ }
108
120
}
109
121
110
122
// ErrorStack writes v along with call stack into error log.
111
123
func ErrorStack (v ... any ) {
112
- // there is newline in stack string
113
- writeStack (fmt .Sprint (v ... ))
124
+ if shallLog (ErrorLevel ) {
125
+ // there is newline in stack string
126
+ writeStack (fmt .Sprint (v ... ))
127
+ }
114
128
}
115
129
116
130
// ErrorStackf writes v along with call stack in format into error log.
117
131
func ErrorStackf (format string , v ... any ) {
118
- // there is newline in stack string
119
- writeStack (fmt .Sprintf (format , v ... ))
132
+ if shallLog (ErrorLevel ) {
133
+ // there is newline in stack string
134
+ writeStack (fmt .Sprintf (format , v ... ))
135
+ }
120
136
}
121
137
122
138
// Errorv writes v into error log with json content.
123
139
// No call stack attached, because not elegant to pack the messages.
124
140
func Errorv (v any ) {
125
- writeError (v )
141
+ if shallLog (ErrorLevel ) {
142
+ writeError (v )
143
+ }
126
144
}
127
145
128
146
// Errorw writes msg along with fields into error log.
129
147
func Errorw (msg string , fields ... LogField ) {
130
- writeError (msg , fields ... )
148
+ if shallLog (ErrorLevel ) {
149
+ writeError (msg , fields ... )
150
+ }
131
151
}
132
152
133
153
// Field returns a LogField for the given key and value.
@@ -170,22 +190,30 @@ func Field(key string, value any) LogField {
170
190
171
191
// Info writes v into access log.
172
192
func Info (v ... any ) {
173
- writeInfo (fmt .Sprint (v ... ))
193
+ if shallLog (InfoLevel ) {
194
+ writeInfo (fmt .Sprint (v ... ))
195
+ }
174
196
}
175
197
176
198
// Infof writes v with format into access log.
177
199
func Infof (format string , v ... any ) {
178
- writeInfo (fmt .Sprintf (format , v ... ))
200
+ if shallLog (InfoLevel ) {
201
+ writeInfo (fmt .Sprintf (format , v ... ))
202
+ }
179
203
}
180
204
181
205
// Infov writes v into access log with json content.
182
206
func Infov (v any ) {
183
- writeInfo (v )
207
+ if shallLog (InfoLevel ) {
208
+ writeInfo (v )
209
+ }
184
210
}
185
211
186
212
// Infow writes msg along with fields into access log.
187
213
func Infow (msg string , fields ... LogField ) {
188
- writeInfo (msg , fields ... )
214
+ if shallLog (InfoLevel ) {
215
+ writeInfo (msg , fields ... )
216
+ }
189
217
}
190
218
191
219
// Must checks if err is nil, otherwise logs the error and exits.
@@ -194,7 +222,7 @@ func Must(err error) {
194
222
return
195
223
}
196
224
197
- msg := err .Error ()
225
+ msg := fmt . Sprintf ( "%+v \n \n %s" , err .Error (), debug . Stack () )
198
226
log .Print (msg )
199
227
getWriter ().Severe (msg )
200
228
@@ -269,42 +297,58 @@ func SetUp(c LogConf) (err error) {
269
297
270
298
// Severe writes v into severe log.
271
299
func Severe (v ... any ) {
272
- writeSevere (fmt .Sprint (v ... ))
300
+ if shallLog (SevereLevel ) {
301
+ writeSevere (fmt .Sprint (v ... ))
302
+ }
273
303
}
274
304
275
305
// Severef writes v with format into severe log.
276
306
func Severef (format string , v ... any ) {
277
- writeSevere (fmt .Sprintf (format , v ... ))
307
+ if shallLog (SevereLevel ) {
308
+ writeSevere (fmt .Sprintf (format , v ... ))
309
+ }
278
310
}
279
311
280
312
// Slow writes v into slow log.
281
313
func Slow (v ... any ) {
282
- writeSlow (fmt .Sprint (v ... ))
314
+ if shallLog (ErrorLevel ) {
315
+ writeSlow (fmt .Sprint (v ... ))
316
+ }
283
317
}
284
318
285
319
// Slowf writes v with format into slow log.
286
320
func Slowf (format string , v ... any ) {
287
- writeSlow (fmt .Sprintf (format , v ... ))
321
+ if shallLog (ErrorLevel ) {
322
+ writeSlow (fmt .Sprintf (format , v ... ))
323
+ }
288
324
}
289
325
290
326
// Slowv writes v into slow log with json content.
291
327
func Slowv (v any ) {
292
- writeSlow (v )
328
+ if shallLog (ErrorLevel ) {
329
+ writeSlow (v )
330
+ }
293
331
}
294
332
295
333
// Sloww writes msg along with fields into slow log.
296
334
func Sloww (msg string , fields ... LogField ) {
297
- writeSlow (msg , fields ... )
335
+ if shallLog (ErrorLevel ) {
336
+ writeSlow (msg , fields ... )
337
+ }
298
338
}
299
339
300
340
// Stat writes v into stat log.
301
341
func Stat (v ... any ) {
302
- writeStat (fmt .Sprint (v ... ))
342
+ if shallLogStat () && shallLog (InfoLevel ) {
343
+ writeStat (fmt .Sprint (v ... ))
344
+ }
303
345
}
304
346
305
347
// Statf writes v with format into stat log.
306
348
func Statf (format string , v ... any ) {
307
- writeStat (fmt .Sprintf (format , v ... ))
349
+ if shallLogStat () && shallLog (InfoLevel ) {
350
+ writeStat (fmt .Sprintf (format , v ... ))
351
+ }
308
352
}
309
353
310
354
// WithCooldownMillis customizes logging on writing call stack interval.
@@ -429,44 +473,58 @@ func shallLogStat() bool {
429
473
return atomic .LoadUint32 (& disableStat ) == 0
430
474
}
431
475
476
+ // writeDebug writes v into debug log.
477
+ // Not checking shallLog here is for performance consideration.
478
+ // If we check shallLog here, the fmt.Sprint might be called even if the log level is not enabled.
479
+ // The caller should check shallLog before calling this function.
432
480
func writeDebug (val any , fields ... LogField ) {
433
- if shallLog (DebugLevel ) {
434
- getWriter ().Debug (val , addCaller (fields ... )... )
435
- }
481
+ getWriter ().Debug (val , addCaller (fields ... )... )
436
482
}
437
483
484
+ // writeError writes v into error log.
485
+ // Not checking shallLog here is for performance consideration.
486
+ // If we check shallLog here, the fmt.Sprint might be called even if the log level is not enabled.
487
+ // The caller should check shallLog before calling this function.
438
488
func writeError (val any , fields ... LogField ) {
439
- if shallLog (ErrorLevel ) {
440
- getWriter ().Error (val , addCaller (fields ... )... )
441
- }
489
+ getWriter ().Error (val , addCaller (fields ... )... )
442
490
}
443
491
492
+ // writeInfo writes v into info log.
493
+ // Not checking shallLog here is for performance consideration.
494
+ // If we check shallLog here, the fmt.Sprint might be called even if the log level is not enabled.
495
+ // The caller should check shallLog before calling this function.
444
496
func writeInfo (val any , fields ... LogField ) {
445
- if shallLog (InfoLevel ) {
446
- getWriter ().Info (val , addCaller (fields ... )... )
447
- }
497
+ getWriter ().Info (val , addCaller (fields ... )... )
448
498
}
449
499
500
+ // writeSevere writes v into severe log.
501
+ // Not checking shallLog here is for performance consideration.
502
+ // If we check shallLog here, the fmt.Sprint might be called even if the log level is not enabled.
503
+ // The caller should check shallLog before calling this function.
450
504
func writeSevere (msg string ) {
451
- if shallLog (SevereLevel ) {
452
- getWriter ().Severe (fmt .Sprintf ("%s\n %s" , msg , string (debug .Stack ())))
453
- }
505
+ getWriter ().Severe (fmt .Sprintf ("%s\n %s" , msg , string (debug .Stack ())))
454
506
}
455
507
508
+ // writeSlow writes v into slow log.
509
+ // Not checking shallLog here is for performance consideration.
510
+ // If we check shallLog here, the fmt.Sprint might be called even if the log level is not enabled.
511
+ // The caller should check shallLog before calling this function.
456
512
func writeSlow (val any , fields ... LogField ) {
457
- if shallLog (ErrorLevel ) {
458
- getWriter ().Slow (val , addCaller (fields ... )... )
459
- }
513
+ getWriter ().Slow (val , addCaller (fields ... )... )
460
514
}
461
515
516
+ // writeStack writes v into stack log.
517
+ // Not checking shallLog here is for performance consideration.
518
+ // If we check shallLog here, the fmt.Sprint might be called even if the log level is not enabled.
519
+ // The caller should check shallLog before calling this function.
462
520
func writeStack (msg string ) {
463
- if shallLog (ErrorLevel ) {
464
- getWriter ().Stack (fmt .Sprintf ("%s\n %s" , msg , string (debug .Stack ())))
465
- }
521
+ getWriter ().Stack (fmt .Sprintf ("%s\n %s" , msg , string (debug .Stack ())))
466
522
}
467
523
524
+ // writeStat writes v into stat log.
525
+ // Not checking shallLog here is for performance consideration.
526
+ // If we check shallLog here, the fmt.Sprint might be called even if the log level is not enabled.
527
+ // The caller should check shallLog before calling this function.
468
528
func writeStat (msg string ) {
469
- if shallLogStat () && shallLog (InfoLevel ) {
470
- getWriter ().Stat (msg , addCaller ()... )
471
- }
529
+ getWriter ().Stat (msg , addCaller ()... )
472
530
}
0 commit comments