-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.go
More file actions
199 lines (167 loc) · 4.56 KB
/
handler.go
File metadata and controls
199 lines (167 loc) · 4.56 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package ctxslog
import (
"context"
"encoding/json"
"fmt"
"log/slog"
"math"
"runtime"
)
// Minimal and maximal possible log levels.
//
// You can use MinLevel as the log level or callstack level in context/logger to
// include all logs, or use MaxLevel to exclude all logs (except logs logged
// explicitly at MaxLevel).
const (
MinLevel = slog.Level(math.MinInt)
MaxLevel = slog.Level(math.MaxInt)
)
type logKeyType struct{}
var logKey logKeyType
type logLevelType struct{}
var logLevelKey logLevelType
type callstackLevelType struct{}
var callstackLevelKey callstackLevelType
// Attaches logger args into context.
//
// NOTE: This does in most cases require that you already called slog.SetDefault
// on a logger retruend by New.
func Attach(ctx context.Context, args ...any) context.Context {
logger := slog.Default()
if l, ok := ctx.Value(logKey).(*slog.Logger); ok {
logger = l
}
return context.WithValue(ctx, logKey, logger.With(args...))
}
// AttachLogLevel attaches min log level (inclusive) to the context,
// overriding the global one set on the logger.
func AttachLogLevel(ctx context.Context, level slog.Leveler) context.Context {
return context.WithValue(ctx, logLevelKey, level)
}
// AttachCallstackLevel attaches min callstack level (inclusive) to the context,
// overriding the global one set on the logger.
func AttachCallstackLevel(ctx context.Context, level slog.Leveler) context.Context {
return context.WithValue(ctx, callstackLevelKey, level)
}
type ctxHandler struct {
h slog.Handler
}
func (ch ctxHandler) Handle(ctx context.Context, r slog.Record) error {
if l, ok := ctx.Value(logKey).(*slog.Logger); ok && l != nil {
// override the logger in context to avoid infinite recursion
ctx := context.WithValue(ctx, logKey, (*slog.Logger)(nil))
return l.Handler().Handle(ctx, r)
}
return ch.h.Handle(ctx, r)
}
func (ch ctxHandler) Enabled(ctx context.Context, l slog.Level) bool {
if level, _ := ctx.Value(logLevelKey).(slog.Leveler); level != nil {
return l >= level.Level()
}
return ch.h.Enabled(ctx, l)
}
func (ch ctxHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
return &ctxHandler{h: ch.h.WithAttrs(attrs)}
}
func (ch ctxHandler) WithGroup(name string) slog.Handler {
return &ctxHandler{h: ch.h.WithGroup(name)}
}
// ContextHandler wraps handler to handle contexts from Attach and
// AttachLogLevel.
func ContextHandler(h slog.Handler) slog.Handler {
if _, ok := h.(*ctxHandler); ok {
// avoid double wrapping
return h
}
return &ctxHandler{h: h}
}
type callstackHandler struct {
h slog.Handler
level slog.Leveler
}
func (ch *callstackHandler) Enabled(ctx context.Context, l slog.Level) bool {
return ch.h.Enabled(ctx, l)
}
func (ch *callstackHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
return &callstackHandler{
h: ch.h.WithAttrs(attrs),
level: ch.level,
}
}
func (ch *callstackHandler) WithGroup(name string) slog.Handler {
return &callstackHandler{
h: ch.h.WithGroup(name),
level: ch.level,
}
}
func (ch *callstackHandler) Handle(ctx context.Context, r slog.Record) error {
level, _ := ctx.Value(callstackLevelKey).(slog.Leveler)
if level == nil {
level = ch.level
}
if r.Level >= level.Level() && r.PC != 0 {
var pcs []uintptr
max := 20
for {
pcs = make([]uintptr, max)
n := runtime.Callers(0, pcs)
if n < max {
pcs = pcs[:n]
break
}
max += max
}
// Skip everything before r.PC if possible.
// Those are mostly just internal slog related wrappers.
for i, pc := range pcs {
if pc == r.PC {
pcs = pcs[i:]
break
}
}
if len(pcs) > 0 {
r = r.Clone()
r.AddAttrs(slog.Any("callstack", callstack(pcs)))
}
}
return ch.h.Handle(ctx, r)
}
type wrapSource slog.Source
func (ws *wrapSource) MarshalJSON() ([]byte, error) {
return json.Marshal((*slog.Source)(ws))
}
func (ws *wrapSource) String() string {
return fmt.Sprintf("%s:%d", ws.File, ws.Line)
}
func callstack(pcs []uintptr) []*wrapSource {
fs := runtime.CallersFrames(pcs)
stack := make([]*wrapSource, 0, len(pcs))
for {
f, next := fs.Next()
stack = append(stack, &wrapSource{
Function: f.Function,
File: f.File,
Line: f.Line,
})
if !next {
break
}
}
return stack
}
// CallstackHandler wraps handler to print out full callstack at minimal level
// (inclusive).
//
// If h is already a CallstackHandler,
// its configured min level will be modified instead.
func CallstackHandler(h slog.Handler, min slog.Leveler) slog.Handler {
if ch, ok := h.(*callstackHandler); ok {
// avoid double wrapping
ch.level = min
return ch
}
return &callstackHandler{
h: h,
level: min,
}
}