Skip to content

Commit d671f33

Browse files
committed
👔 up: update logger handler, remove defaultH setting.
- update logger new logic, can be with config funcs
1 parent 4799e4b commit d671f33

File tree

3 files changed

+53
-107
lines changed

3 files changed

+53
-107
lines changed

logger.go

+37-83
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ type SLogger interface {
1616
Logf(level Level, format string, v ...any)
1717
}
1818

19+
// LoggerFn func
20+
type LoggerFn func(l *Logger)
21+
1922
// Logger log dispatcher definition.
2023
//
2124
// The logger implements the `github.com/gookit/gsr.Logger`
@@ -26,8 +29,7 @@ type Logger struct {
2629
// log latest error
2730
err error
2831

29-
// default handler for logger
30-
defaultH Handler
32+
// log handlers for logger
3133
handlers []Handler
3234
processors []Processor
3335

@@ -59,20 +61,20 @@ func New() *Logger {
5961
return NewWithName("logger")
6062
}
6163

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-
6764
// NewWithHandlers create a new logger with handlers
6865
func NewWithHandlers(hs ...Handler) *Logger {
6966
logger := NewWithName("logger")
7067
logger.AddHandlers(hs...)
7168
return logger
7269
}
7370

71+
// NewWithConfig create a new logger with config func
72+
func NewWithConfig(fns ...LoggerFn) *Logger {
73+
return NewWithName("logger", fns...)
74+
}
75+
7476
// NewWithName create a new logger with name
75-
func NewWithName(name string) *Logger {
77+
func NewWithName(name string, fns ...LoggerFn) *Logger {
7678
logger := &Logger{
7779
name: name,
7880
// exit handle
@@ -88,7 +90,7 @@ func NewWithName(name string) *Logger {
8890
logger.recordPool.New = func() interface{} {
8991
return newRecord(logger)
9092
}
91-
return logger
93+
return logger.Config(fns...)
9294
}
9395

9496
// NewRecord get new logger record
@@ -115,18 +117,15 @@ func (l *Logger) releaseRecord(r *Record) {
115117
const flushInterval = 30 * time.Second
116118

117119
// Config current logger
118-
func (l *Logger) Config(fns ...func(l *Logger)) *Logger {
120+
func (l *Logger) Config(fns ...LoggerFn) *Logger {
119121
for _, fn := range fns {
120122
fn(l)
121123
}
122124
return l
123125
}
124126

125127
// 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) }
130129

131130
// FlushDaemon run flush handle on daemon
132131
//
@@ -219,12 +218,6 @@ func (l *Logger) Close() error {
219218

220219
// VisitAll logger handlers
221220
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-
228221
for _, handler := range l.handlers {
229222
// you can return nil for ignore error
230223
if err := fn(handler); err != nil {
@@ -289,9 +282,6 @@ func (l *Logger) LastErr() error {
289282
// ---------------------------------------------------------------------------
290283
//
291284

292-
// SetDefault handler to the logger
293-
func (l *Logger) SetDefault(h Handler) { l.defaultH = h }
294-
295285
// AddHandler to the logger
296286
func (l *Logger) AddHandler(h Handler) { l.PushHandlers(h) }
297287

@@ -303,25 +293,13 @@ func (l *Logger) PushHandler(h Handler) { l.PushHandlers(h) }
303293

304294
// PushHandlers to the logger
305295
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-
312296
if len(hs) > 0 {
313297
l.handlers = append(l.handlers, hs...)
314298
}
315299
}
316300

317301
// SetHandlers for the logger
318302
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-
325303
l.handlers = hs
326304
}
327305

@@ -347,39 +325,34 @@ func (l *Logger) SetProcessors(ps []Processor) { l.processors = ps }
347325
// Record return a new record for log
348326
func (l *Logger) Record() *Record {
349327
r := l.newRecord()
350-
351328
defer l.releaseRecord(r)
352329
return r
353330
}
354331

355332
// WithField new record with field
356333
func (l *Logger) WithField(name string, value any) *Record {
357334
r := l.newRecord()
358-
359335
defer l.releaseRecord(r)
360336
return r.WithField(name, value)
361337
}
362338

363339
// WithFields new record with fields
364340
func (l *Logger) WithFields(fields M) *Record {
365341
r := l.newRecord()
366-
367342
defer l.releaseRecord(r)
368343
return r.WithFields(fields)
369344
}
370345

371346
// WithData new record with data
372347
func (l *Logger) WithData(data M) *Record {
373348
r := l.newRecord()
374-
375349
defer l.releaseRecord(r)
376350
return r.WithData(data)
377351
}
378352

379353
// WithTime new record with time.Time
380354
func (l *Logger) WithTime(t time.Time) *Record {
381355
r := l.newRecord()
382-
383356
defer l.releaseRecord(r)
384357
return r.WithTime(t)
385358
}
@@ -390,7 +363,6 @@ func (l *Logger) WithCtx(ctx context.Context) *Record { return l.WithContext(ctx
390363
// WithContext new record with context.Context
391364
func (l *Logger) WithContext(ctx context.Context) *Record {
392365
r := l.newRecord()
393-
394366
defer l.releaseRecord(r)
395367
return r.WithContext(ctx)
396368
}
@@ -431,86 +403,68 @@ func (l *Logger) Print(args ...any) { l.log(PrintLevel, args) }
431403
func (l *Logger) Println(args ...any) { l.log(PrintLevel, args) }
432404

433405
// 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) }
437407

438408
// Warn logs a message at level Warn
439409
func (l *Logger) Warn(args ...any) { l.log(WarnLevel, args) }
440410

441411
// 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) }
445413

446-
// Warning logs a message at level Warn
414+
// Warning logs a message at level Warn, alias of Logger.Warn()
447415
func (l *Logger) Warning(args ...any) { l.log(WarnLevel, args) }
448416

449417
// Info logs a message at level Info
450418
func (l *Logger) Info(args ...any) { l.log(InfoLevel, args) }
451419

452420
// 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) }
456422

457-
// Trace logs a message at level Trace
423+
// Trace logs a message at level trace
458424
func (l *Logger) Trace(args ...any) { l.log(TraceLevel, args) }
459425

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) }
464428

465429
// Error logs a message at level error
466430
func (l *Logger) Error(args ...any) { l.log(ErrorLevel, args) }
467431

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) }
472434

473-
// ErrorT logs a error type at level Error
435+
// ErrorT logs a error type at level error
474436
func (l *Logger) ErrorT(err error) {
475437
if err != nil {
476438
l.log(ErrorLevel, []any{err})
477439
}
478440
}
479441

480-
// Notice logs a message at level Notice
442+
// Notice logs a message at level notice
481443
func (l *Logger) Notice(args ...any) { l.log(NoticeLevel, args) }
482444

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) }
487447

488-
// Debug logs a message at level Debug
448+
// Debug logs a message at level debug
489449
func (l *Logger) Debug(args ...any) { l.log(DebugLevel, args) }
490450

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) }
495453

496-
// Fatal logs a message at level Fatal
454+
// Fatal logs a message at level fatal
497455
func (l *Logger) Fatal(args ...any) { l.log(FatalLevel, args) }
498456

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) }
503459

504-
// Fatalln logs a message at level Fatal
460+
// Fatalln logs a message at level fatal
505461
func (l *Logger) Fatalln(args ...any) { l.log(FatalLevel, args) }
506462

507-
// Panic logs a message at level Panic
463+
// Panic logs a message at level panic
508464
func (l *Logger) Panic(args ...any) { l.log(PanicLevel, args) }
509465

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) }
514468

515-
// Panicln logs a message at level Panic
469+
// Panicln logs a message at level panic
516470
func (l *Logger) Panicln(args ...any) { l.log(PanicLevel, args) }

sugared.go

+16-12
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import (
77
"github.com/gookit/color"
88
)
99

10+
// SugaredLoggerFn func type.
11+
type SugaredLoggerFn func(sl *SugaredLogger)
12+
1013
// SugaredLogger definition.
1114
// Is a fast and usable Logger, which already contains
1215
// the default formatting and handling capabilities
@@ -22,7 +25,7 @@ type SugaredLogger struct {
2225

2326
// NewStdLogger instance
2427
func NewStdLogger() *SugaredLogger {
25-
return NewSugaredLogger(os.Stdout, DebugLevel).Config(func(sl *SugaredLogger) {
28+
return NewSugaredLogger(os.Stdout, DebugLevel, func(sl *SugaredLogger) {
2629
sl.SetName("stdLogger")
2730
// sl.CallerSkip += 1
2831
sl.ReportCaller = true
@@ -32,7 +35,7 @@ func NewStdLogger() *SugaredLogger {
3235
}
3336

3437
// NewSugaredLogger create new SugaredLogger
35-
func NewSugaredLogger(output io.Writer, level Level) *SugaredLogger {
38+
func NewSugaredLogger(output io.Writer, level Level, fns ...SugaredLoggerFn) *SugaredLogger {
3639
sl := &SugaredLogger{
3740
Level: level,
3841
Output: output,
@@ -42,30 +45,31 @@ func NewSugaredLogger(output io.Writer, level Level) *SugaredLogger {
4245
}
4346

4447
// NOTICE: use self as an log handler
45-
sl.SetDefault(sl)
48+
sl.AddHandler(sl)
4649

47-
return sl
50+
return sl.Config(fns...)
4851
}
4952

5053
// NewJSONSugared create new SugaredLogger with JSONFormatter
51-
func NewJSONSugared(out io.Writer, level Level) *SugaredLogger {
54+
func NewJSONSugared(out io.Writer, level Level, fns ...SugaredLoggerFn) *SugaredLogger {
5255
sl := NewSugaredLogger(out, level)
5356
sl.Formatter = NewJSONFormatter()
5457

55-
return sl
58+
return sl.Config(fns...)
5659
}
5760

5861
// Config current logger
59-
func (sl *SugaredLogger) Config(fn func(sl *SugaredLogger)) *SugaredLogger {
60-
fn(sl)
62+
func (sl *SugaredLogger) Config(fns ...SugaredLoggerFn) *SugaredLogger {
63+
for _, fn := range fns {
64+
fn(sl)
65+
}
6166
return sl
6267
}
6368

6469
// Configure current logger
65-
func (sl *SugaredLogger) Configure(fn func(sl *SugaredLogger)) *SugaredLogger {
66-
fn(sl)
67-
return sl
68-
}
70+
//
71+
// Deprecated: please use SugaredLogger.Config()
72+
func (sl *SugaredLogger) Configure(fn SugaredLoggerFn) *SugaredLogger { return sl.Config(fn) }
6973

7074
// Reset the logger
7175
func (sl *SugaredLogger) Reset() {

write.go

-12
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,6 @@ func (l *Logger) writeRecord(level Level, r *Record) {
6161

6262
// do write log message
6363
var inited bool
64-
if l.defaultH != nil && l.defaultH.IsHandling(level) {
65-
if !inited {
66-
r.Init(l.LowerLevelName)
67-
r.beforeHandle(l)
68-
inited = true
69-
}
70-
71-
if err := l.defaultH.Handle(r); err != nil {
72-
printlnStderr("slog: failed to handle log, error: ", err)
73-
}
74-
}
75-
7664
for _, handler := range l.handlers {
7765
if handler.IsHandling(level) {
7866
if !inited {

0 commit comments

Comments
 (0)