Skip to content

Commit 22bcdab

Browse files
committed
refactoring logger
Signed-off-by: Markus Blaschke <[email protected]>
1 parent e53a531 commit 22bcdab

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

logger/logger.go renamed to log/slogger/slog.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
package logger
1+
package slogger
22

33
import (
44
"context"
55
"log/slog"
6+
"os"
67
)
78

89
const (
@@ -36,13 +37,16 @@ func NewHandlerOptions(handler *slog.HandlerOptions) *HandlerOptions {
3637

3738
ret := &HandlerOptions{HandlerOptions: handler}
3839

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+
}
4145

4246
return ret
4347
}
4448

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 {
4650
return func(groups []string, a slog.Attr) slog.Attr {
4751
switch a.Key {
4852
case slog.LevelKey:
@@ -58,11 +62,7 @@ func NewReplaceAttr(handler *HandlerOptions, existingFunc *func(groups []string,
5862
}
5963
}
6064

61-
if existingFunc != nil {
62-
return (*existingFunc)(groups, a)
63-
} else {
64-
return a
65-
}
65+
return callback(groups, a)
6666
}
6767
}
6868

@@ -77,6 +77,7 @@ func (l *Logger) Trace(msg string, fields ...any) {
7777

7878
func (l *Logger) Fatal(msg string, fields ...any) {
7979
l.Log(context.Background(), LevelFatal, msg, fields...)
80+
os.Exit(1)
8081
}
8182

8283
func (l *Logger) Panic(msg string, fields ...any) {

0 commit comments

Comments
 (0)