@@ -16,6 +16,9 @@ type SLogger interface {
16
16
Logf (level Level , format string , v ... any )
17
17
}
18
18
19
+ // LoggerFn func
20
+ type LoggerFn func (l * Logger )
21
+
19
22
// Logger log dispatcher definition.
20
23
//
21
24
// The logger implements the `github.com/gookit/gsr.Logger`
@@ -26,8 +29,7 @@ type Logger struct {
26
29
// log latest error
27
30
err error
28
31
29
- // default handler for logger
30
- defaultH Handler
32
+ // log handlers for logger
31
33
handlers []Handler
32
34
processors []Processor
33
35
@@ -59,20 +61,20 @@ func New() *Logger {
59
61
return NewWithName ("logger" )
60
62
}
61
63
62
- // NewWithConfig create a new logger with config func
63
- func NewWithConfig (fn func (l * Logger )) * Logger {
64
- return NewWithName ("logger" ).Configure (fn )
65
- }
66
-
67
64
// NewWithHandlers create a new logger with handlers
68
65
func NewWithHandlers (hs ... Handler ) * Logger {
69
66
logger := NewWithName ("logger" )
70
67
logger .AddHandlers (hs ... )
71
68
return logger
72
69
}
73
70
71
+ // NewWithConfig create a new logger with config func
72
+ func NewWithConfig (fns ... LoggerFn ) * Logger {
73
+ return NewWithName ("logger" , fns ... )
74
+ }
75
+
74
76
// NewWithName create a new logger with name
75
- func NewWithName (name string ) * Logger {
77
+ func NewWithName (name string , fns ... LoggerFn ) * Logger {
76
78
logger := & Logger {
77
79
name : name ,
78
80
// exit handle
@@ -88,7 +90,7 @@ func NewWithName(name string) *Logger {
88
90
logger .recordPool .New = func () interface {} {
89
91
return newRecord (logger )
90
92
}
91
- return logger
93
+ return logger . Config ( fns ... )
92
94
}
93
95
94
96
// NewRecord get new logger record
@@ -115,18 +117,15 @@ func (l *Logger) releaseRecord(r *Record) {
115
117
const flushInterval = 30 * time .Second
116
118
117
119
// Config current logger
118
- func (l * Logger ) Config (fns ... func ( l * Logger ) ) * Logger {
120
+ func (l * Logger ) Config (fns ... LoggerFn ) * Logger {
119
121
for _ , fn := range fns {
120
122
fn (l )
121
123
}
122
124
return l
123
125
}
124
126
125
127
// Configure current logger
126
- func (l * Logger ) Configure (fn func (l * Logger )) * Logger {
127
- fn (l )
128
- return l
129
- }
128
+ func (l * Logger ) Configure (fn LoggerFn ) * Logger { return l .Config (fn ) }
130
129
131
130
// FlushDaemon run flush handle on daemon
132
131
//
@@ -219,12 +218,6 @@ func (l *Logger) Close() error {
219
218
220
219
// VisitAll logger handlers
221
220
func (l * Logger ) VisitAll (fn func (handler Handler ) error ) error {
222
- if l .defaultH != nil {
223
- if err := fn (l .defaultH ); err != nil {
224
- return err
225
- }
226
- }
227
-
228
221
for _ , handler := range l .handlers {
229
222
// you can return nil for ignore error
230
223
if err := fn (handler ); err != nil {
@@ -289,9 +282,6 @@ func (l *Logger) LastErr() error {
289
282
// ---------------------------------------------------------------------------
290
283
//
291
284
292
- // SetDefault handler to the logger
293
- func (l * Logger ) SetDefault (h Handler ) { l .defaultH = h }
294
-
295
285
// AddHandler to the logger
296
286
func (l * Logger ) AddHandler (h Handler ) { l .PushHandlers (h ) }
297
287
@@ -303,25 +293,13 @@ func (l *Logger) PushHandler(h Handler) { l.PushHandlers(h) }
303
293
304
294
// PushHandlers to the logger
305
295
func (l * Logger ) PushHandlers (hs ... Handler ) {
306
- // if not set default, use first handler
307
- if l .defaultH == nil {
308
- l .SetDefault (hs [0 ])
309
- hs = hs [1 :]
310
- }
311
-
312
296
if len (hs ) > 0 {
313
297
l .handlers = append (l .handlers , hs ... )
314
298
}
315
299
}
316
300
317
301
// SetHandlers for the logger
318
302
func (l * Logger ) SetHandlers (hs []Handler ) {
319
- // if not set default, use first handler
320
- if l .defaultH == nil {
321
- l .SetDefault (hs [0 ])
322
- hs = hs [1 :]
323
- }
324
-
325
303
l .handlers = hs
326
304
}
327
305
@@ -347,39 +325,34 @@ func (l *Logger) SetProcessors(ps []Processor) { l.processors = ps }
347
325
// Record return a new record for log
348
326
func (l * Logger ) Record () * Record {
349
327
r := l .newRecord ()
350
-
351
328
defer l .releaseRecord (r )
352
329
return r
353
330
}
354
331
355
332
// WithField new record with field
356
333
func (l * Logger ) WithField (name string , value any ) * Record {
357
334
r := l .newRecord ()
358
-
359
335
defer l .releaseRecord (r )
360
336
return r .WithField (name , value )
361
337
}
362
338
363
339
// WithFields new record with fields
364
340
func (l * Logger ) WithFields (fields M ) * Record {
365
341
r := l .newRecord ()
366
-
367
342
defer l .releaseRecord (r )
368
343
return r .WithFields (fields )
369
344
}
370
345
371
346
// WithData new record with data
372
347
func (l * Logger ) WithData (data M ) * Record {
373
348
r := l .newRecord ()
374
-
375
349
defer l .releaseRecord (r )
376
350
return r .WithData (data )
377
351
}
378
352
379
353
// WithTime new record with time.Time
380
354
func (l * Logger ) WithTime (t time.Time ) * Record {
381
355
r := l .newRecord ()
382
-
383
356
defer l .releaseRecord (r )
384
357
return r .WithTime (t )
385
358
}
@@ -390,7 +363,6 @@ func (l *Logger) WithCtx(ctx context.Context) *Record { return l.WithContext(ctx
390
363
// WithContext new record with context.Context
391
364
func (l * Logger ) WithContext (ctx context.Context ) * Record {
392
365
r := l .newRecord ()
393
-
394
366
defer l .releaseRecord (r )
395
367
return r .WithContext (ctx )
396
368
}
@@ -431,86 +403,68 @@ func (l *Logger) Print(args ...any) { l.log(PrintLevel, args) }
431
403
func (l * Logger ) Println (args ... any ) { l .log (PrintLevel , args ) }
432
404
433
405
// Printf logs a message at level PrintLevel
434
- func (l * Logger ) Printf (format string , args ... any ) {
435
- l .logf (PrintLevel , format , args )
436
- }
406
+ func (l * Logger ) Printf (format string , args ... any ) { l .logf (PrintLevel , format , args ) }
437
407
438
408
// Warn logs a message at level Warn
439
409
func (l * Logger ) Warn (args ... any ) { l .log (WarnLevel , args ) }
440
410
441
411
// Warnf logs a message at level Warn
442
- func (l * Logger ) Warnf (format string , args ... any ) {
443
- l .logf (WarnLevel , format , args )
444
- }
412
+ func (l * Logger ) Warnf (format string , args ... any ) { l .logf (WarnLevel , format , args ) }
445
413
446
- // Warning logs a message at level Warn
414
+ // Warning logs a message at level Warn, alias of Logger.Warn()
447
415
func (l * Logger ) Warning (args ... any ) { l .log (WarnLevel , args ) }
448
416
449
417
// Info logs a message at level Info
450
418
func (l * Logger ) Info (args ... any ) { l .log (InfoLevel , args ) }
451
419
452
420
// Infof logs a message at level Info
453
- func (l * Logger ) Infof (format string , args ... any ) {
454
- l .logf (InfoLevel , format , args )
455
- }
421
+ func (l * Logger ) Infof (format string , args ... any ) { l .logf (InfoLevel , format , args ) }
456
422
457
- // Trace logs a message at level Trace
423
+ // Trace logs a message at level trace
458
424
func (l * Logger ) Trace (args ... any ) { l .log (TraceLevel , args ) }
459
425
460
- // Tracef logs a message at level Trace
461
- func (l * Logger ) Tracef (format string , args ... any ) {
462
- l .logf (TraceLevel , format , args )
463
- }
426
+ // Tracef logs a message at level trace
427
+ func (l * Logger ) Tracef (format string , args ... any ) { l .logf (TraceLevel , format , args ) }
464
428
465
429
// Error logs a message at level error
466
430
func (l * Logger ) Error (args ... any ) { l .log (ErrorLevel , args ) }
467
431
468
- // Errorf logs a message at level Error
469
- func (l * Logger ) Errorf (format string , args ... any ) {
470
- l .logf (ErrorLevel , format , args )
471
- }
432
+ // Errorf logs a message at level error
433
+ func (l * Logger ) Errorf (format string , args ... any ) { l .logf (ErrorLevel , format , args ) }
472
434
473
- // ErrorT logs a error type at level Error
435
+ // ErrorT logs a error type at level error
474
436
func (l * Logger ) ErrorT (err error ) {
475
437
if err != nil {
476
438
l .log (ErrorLevel , []any {err })
477
439
}
478
440
}
479
441
480
- // Notice logs a message at level Notice
442
+ // Notice logs a message at level notice
481
443
func (l * Logger ) Notice (args ... any ) { l .log (NoticeLevel , args ) }
482
444
483
- // Noticef logs a message at level Notice
484
- func (l * Logger ) Noticef (format string , args ... any ) {
485
- l .logf (NoticeLevel , format , args )
486
- }
445
+ // Noticef logs a message at level notice
446
+ func (l * Logger ) Noticef (format string , args ... any ) { l .logf (NoticeLevel , format , args ) }
487
447
488
- // Debug logs a message at level Debug
448
+ // Debug logs a message at level debug
489
449
func (l * Logger ) Debug (args ... any ) { l .log (DebugLevel , args ) }
490
450
491
- // Debugf logs a message at level Debug
492
- func (l * Logger ) Debugf (format string , args ... any ) {
493
- l .logf (DebugLevel , format , args )
494
- }
451
+ // Debugf logs a message at level debug
452
+ func (l * Logger ) Debugf (format string , args ... any ) { l .logf (DebugLevel , format , args ) }
495
453
496
- // Fatal logs a message at level Fatal
454
+ // Fatal logs a message at level fatal
497
455
func (l * Logger ) Fatal (args ... any ) { l .log (FatalLevel , args ) }
498
456
499
- // Fatalf logs a message at level Fatal
500
- func (l * Logger ) Fatalf (format string , args ... any ) {
501
- l .logf (FatalLevel , format , args )
502
- }
457
+ // Fatalf logs a message at level fatal
458
+ func (l * Logger ) Fatalf (format string , args ... any ) { l .logf (FatalLevel , format , args ) }
503
459
504
- // Fatalln logs a message at level Fatal
460
+ // Fatalln logs a message at level fatal
505
461
func (l * Logger ) Fatalln (args ... any ) { l .log (FatalLevel , args ) }
506
462
507
- // Panic logs a message at level Panic
463
+ // Panic logs a message at level panic
508
464
func (l * Logger ) Panic (args ... any ) { l .log (PanicLevel , args ) }
509
465
510
- // Panicf logs a message at level Panic
511
- func (l * Logger ) Panicf (format string , args ... any ) {
512
- l .logf (PanicLevel , format , args )
513
- }
466
+ // Panicf logs a message at level panic
467
+ func (l * Logger ) Panicf (format string , args ... any ) { l .logf (PanicLevel , format , args ) }
514
468
515
- // Panicln logs a message at level Panic
469
+ // Panicln logs a message at level panic
516
470
func (l * Logger ) Panicln (args ... any ) { l .log (PanicLevel , args ) }
0 commit comments