1
- package logger
1
+ package slogger
2
2
3
3
import (
4
4
"context"
5
5
"log/slog"
6
+ "os"
6
7
)
7
8
8
9
const (
@@ -36,13 +37,16 @@ func NewHandlerOptions(handler *slog.HandlerOptions) *HandlerOptions {
36
37
37
38
ret := & HandlerOptions {HandlerOptions : handler }
38
39
39
- existingFunc := & handler .ReplaceAttr
40
- handler .ReplaceAttr = NewReplaceAttr (ret , existingFunc )
40
+ if handler .ReplaceAttr != nil {
41
+ handler .ReplaceAttr = NewReplaceAttr (ret , handler .ReplaceAttr )
42
+ } else {
43
+ handler .ReplaceAttr = NewReplaceAttr (ret , func (groups []string , a slog.Attr ) slog.Attr { return a })
44
+ }
41
45
42
46
return ret
43
47
}
44
48
45
- func NewReplaceAttr (handler * HandlerOptions , existingFunc * func (groups []string , a slog.Attr ) slog.Attr ) func (groups []string , a slog.Attr ) slog.Attr {
49
+ func NewReplaceAttr (handler * HandlerOptions , callback func (groups []string , a slog.Attr ) slog.Attr ) func (groups []string , a slog.Attr ) slog.Attr {
46
50
return func (groups []string , a slog.Attr ) slog.Attr {
47
51
switch a .Key {
48
52
case slog .LevelKey :
@@ -58,11 +62,7 @@ func NewReplaceAttr(handler *HandlerOptions, existingFunc *func(groups []string,
58
62
}
59
63
}
60
64
61
- if existingFunc != nil {
62
- return (* existingFunc )(groups , a )
63
- } else {
64
- return a
65
- }
65
+ return callback (groups , a )
66
66
}
67
67
}
68
68
@@ -77,6 +77,7 @@ func (l *Logger) Trace(msg string, fields ...any) {
77
77
78
78
func (l * Logger ) Fatal (msg string , fields ... any ) {
79
79
l .Log (context .Background (), LevelFatal , msg , fields ... )
80
+ os .Exit (1 )
80
81
}
81
82
82
83
func (l * Logger ) Panic (msg string , fields ... any ) {
0 commit comments