/
slog.go
80 lines (63 loc) · 1.51 KB
/
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package logger
import (
"context"
"log/slog"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var levelMap = map[slog.Level]zapcore.Level{
slog.LevelDebug: zapcore.DebugLevel,
slog.LevelInfo: zapcore.InfoLevel,
slog.LevelWarn: zapcore.WarnLevel,
slog.LevelError: zapcore.ErrorLevel,
}
type ZapHandler struct {
config zap.Config
logger *zap.Logger
attrs []slog.Attr
group string
}
func (h *ZapHandler) Enabled(_ context.Context, level slog.Level) bool {
return levelMap[level] >= h.config.Level.Level()
}
func (h *ZapHandler) Handle(ctx context.Context, record slog.Record) error {
fields := []zapcore.Field{}
for _, attr := range h.attrs {
fields = append(fields, zap.Any(attr.Key, attr.Value))
}
record.Attrs(func(a slog.Attr) bool {
fields = append(fields, zap.Any(a.Key, a.Value))
return true
})
if ce := h.logger.Check(levelMap[record.Level], record.Message); ce != nil {
ce.Write(fields...)
}
return nil
}
func (h *ZapHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
groupPrepend := ""
if len(h.group) > 0 {
groupPrepend = h.group + "."
}
newattrs := append([]slog.Attr{}, h.attrs...)
for _, attr := range attrs {
newattrs = append(newattrs, slog.Attr{
Key: groupPrepend + attr.Key,
Value: attr.Value,
})
}
return &ZapHandler{
config: h.config,
logger: h.logger,
attrs: newattrs,
group: h.group,
}
}
func (h *ZapHandler) WithGroup(name string) slog.Handler {
return &ZapHandler{
config: h.config,
logger: h.logger,
attrs: h.attrs,
group: name,
}
}