-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhandler.go
112 lines (91 loc) · 2.47 KB
/
handler.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package slogzap
import (
"context"
"runtime"
"log/slog"
slogcommon "github.com/samber/slog-common"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
type Option struct {
// log level (default: debug)
Level slog.Leveler
// optional: zap logger (default: zap.L())
Logger *zap.Logger
// optional: fetch attributes from context
AttrFromContext []func(ctx context.Context) []slog.Attr
// optional: customize json payload builder
Converter Converter
// optional: see slog.HandlerOptions
AddSource bool
ReplaceAttr func(groups []string, a slog.Attr) slog.Attr
}
func (o Option) NewZapHandler() slog.Handler {
if o.Level == nil {
o.Level = slog.LevelDebug
}
if o.Logger == nil {
// should be selected lazily ?
o.Logger = zap.L()
}
if o.AttrFromContext == nil {
o.AttrFromContext = []func(ctx context.Context) []slog.Attr{}
}
return &ZapHandler{
option: o,
attrs: []slog.Attr{},
groups: []string{},
}
}
var _ slog.Handler = (*ZapHandler)(nil)
type ZapHandler struct {
option Option
attrs []slog.Attr
groups []string
}
func (h *ZapHandler) Enabled(_ context.Context, level slog.Level) bool {
return level >= h.option.Level.Level()
}
func (h *ZapHandler) Handle(ctx context.Context, record slog.Record) error {
converter := DefaultConverter
if h.option.Converter != nil {
converter = h.option.Converter
}
level := LogLevels[record.Level]
fromContext := slogcommon.ContextExtractor(ctx, h.option.AttrFromContext)
fields := converter(h.option.AddSource, h.option.ReplaceAttr, append(h.attrs, fromContext...), h.groups, &record)
checked := h.option.Logger.Check(level, record.Message)
if checked != nil {
if h.option.AddSource {
frame, _ := runtime.CallersFrames([]uintptr{record.PC}).Next()
checked.Caller = zapcore.NewEntryCaller(0, frame.File, frame.Line, true)
checked.Stack = "" //@TODO
} else {
checked.Caller = zapcore.EntryCaller{}
checked.Stack = ""
}
checked.Write(fields...)
return nil
} else {
h.option.Logger.Log(level, record.Message, fields...)
}
return nil
}
func (h *ZapHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
return &ZapHandler{
option: h.option,
attrs: slogcommon.AppendAttrsToGroup(h.groups, h.attrs, attrs...),
groups: h.groups,
}
}
func (h *ZapHandler) WithGroup(name string) slog.Handler {
// https://cs.opensource.google/go/x/exp/+/46b07846:slog/handler.go;l=247
if name == "" {
return h
}
return &ZapHandler{
option: h.option,
attrs: h.attrs,
groups: append(h.groups, name),
}
}