-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
slog.go
49 lines (40 loc) · 999 Bytes
/
slog.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package logger
import (
"context"
"log/slog"
)
type StructLogger struct {
Logger
slog.Handler
}
func NewStructLogger(log Logger) (*slog.Logger, error) {
structLogger := &StructLogger{
Logger: log,
}
return slog.New(structLogger), nil
}
func (sl *StructLogger) Enabled(ctx context.Context, level slog.Level) bool {
return true
}
//nolint:gocritic // This is a wrapper for the logger
func (sl *StructLogger) Handle(ctx context.Context, record slog.Record) error {
switch record.Level {
case slog.LevelDebug:
sl.DebugWithContext(ctx, record.Message)
case slog.LevelInfo:
sl.InfoWithContext(ctx, record.Message)
case slog.LevelWarn:
sl.WarnWithContext(ctx, record.Message)
case slog.LevelError:
sl.ErrorWithContext(ctx, record.Message)
default:
sl.DebugWithContext(ctx, record.Message)
}
return nil
}
func (sl *StructLogger) WithAttrs(_ []slog.Attr) slog.Handler {
return sl.Handler
}
func (sl *StructLogger) WithGroup(_ string) slog.Handler {
return sl.Handler
}