-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathformat_handler.go
More file actions
238 lines (213 loc) · 6.19 KB
/
Copy pathformat_handler.go
File metadata and controls
238 lines (213 loc) · 6.19 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package logger
import (
"context"
"io"
"log/slog"
"runtime"
"slices"
"strconv"
"strings"
"sync"
"time"
)
// FormatHandlerOptions configures a [FormatHandler].
type FormatHandlerOptions struct {
// Level is the minimum enabled logging level.
Level slog.Leveler
// Format is the format string with %placeholder% tokens.
Format string
// TimeLayout is the Go time layout used for the %time% placeholder.
// Defaults to [time.RFC3339] when empty.
TimeLayout string
// LineEnding is appended after each formatted record. Defaults to "\n".
// Set to a non-nil pointer to an empty string to suppress the line ending.
LineEnding *string
}
// FormatHandler is an [slog.Handler] that renders log records according to a
// user-defined format string containing %placeholder% tokens such as %time%,
// %level%, %message%, %attrs%, %logger%, %source_file%, %source_line%, and
// %source_func%.
//
// Unknown placeholders are left in the output verbatim.
type FormatHandler struct {
w io.Writer
level slog.Leveler
format string
timeLayout string
lineEnding string
preAttrs []slog.Attr
groups []string
grpPfx string // cached dot-joined prefix from groups ("g1.g2." or "")
mu *sync.Mutex
// Optimization flags set once at construction to avoid scanning the format
// string on every Handle call.
needsSource bool
needsAttrs bool
needsLogger bool
}
// NewFormatHandler returns a [FormatHandler] that writes to w using the given
// options. The format string is scanned once to determine which placeholders
// are present so that expensive operations (source lookup, attr rendering) can
// be skipped when not needed.
func NewFormatHandler(w io.Writer, opts *FormatHandlerOptions) *FormatHandler {
le := "\n"
if opts.LineEnding != nil {
le = *opts.LineEnding
}
tl := time.RFC3339
if opts.TimeLayout != "" {
tl = opts.TimeLayout
}
f := opts.Format
return &FormatHandler{
w: w,
level: opts.Level,
format: f,
timeLayout: tl,
lineEnding: le,
mu: &sync.Mutex{},
needsSource: strings.Contains(f, "%source_file%") || strings.Contains(f, "%source_line%") || strings.Contains(f, "%source_func%"),
needsAttrs: strings.Contains(f, "%attrs%"),
needsLogger: strings.Contains(f, "%logger%"),
}
}
// Enabled reports whether the handler is enabled for the given level.
func (h *FormatHandler) Enabled(_ context.Context, level slog.Level) bool {
return level >= h.level.Level()
}
// Handle formats and writes a single log record.
func (h *FormatHandler) Handle(_ context.Context, r slog.Record) error {
// Time
var timeStr string
if !r.Time.IsZero() {
timeStr = r.Time.Format(h.timeLayout)
}
// Source (only resolved when needed).
var sourceFile, sourceLine, sourceFunc string
if h.needsSource && r.PC != 0 {
frame, _ := runtime.CallersFrames([]uintptr{r.PC}).Next()
sourceFile = frame.File
sourceLine = strconv.Itoa(frame.Line)
if idx := strings.LastIndex(frame.Function, "."); idx >= 0 {
sourceFunc = frame.Function[idx+1:]
} else {
sourceFunc = frame.Function
}
}
// Attrs — collect pre-attached attrs and record attrs.
var loggerName string
var attrsStr string
if h.needsAttrs || h.needsLogger {
var sb strings.Builder
first := true
appendAttr := func(key string, v slog.Value) {
// Extract logger name and exclude from %attrs%.
if h.needsLogger && key == "logger" {
loggerName = v.String()
return
}
if h.needsAttrs {
if !first {
sb.WriteByte(' ')
}
sb.WriteString(key)
sb.WriteByte('=')
sb.WriteString(formatAttrValue(v))
first = false
}
}
// Pre-attached attrs already have their group prefix baked in.
for _, a := range h.preAttrs {
a.Value = a.Value.Resolve()
if a.Equal(slog.Attr{}) {
continue
}
appendAttr(a.Key, a.Value)
}
// Record attrs get the current full group prefix.
r.Attrs(func(a slog.Attr) bool {
a.Value = a.Value.Resolve()
if !a.Equal(slog.Attr{}) {
appendAttr(h.grpPfx+a.Key, a.Value)
}
return true
})
attrsStr = sb.String()
}
line := strings.NewReplacer(
"%time%", timeStr,
"%level%", r.Level.String(),
"%message%", r.Message,
"%attrs%", attrsStr,
"%logger%", loggerName,
"%source_file%", sourceFile,
"%source_line%", sourceLine,
"%source_func%", sourceFunc,
).Replace(h.format) + h.lineEnding
h.mu.Lock()
defer h.mu.Unlock()
_, err := io.WriteString(h.w, line)
return err
}
// WithAttrs returns a new [FormatHandler] that includes the given attributes in
// every subsequent record. The current group prefix is baked into attr keys so
// that groups added later do not retroactively affect them.
func (h *FormatHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
if len(attrs) == 0 {
return h
}
c := h.clone()
for _, a := range attrs {
c.preAttrs = append(c.preAttrs, slog.Attr{
Key: h.grpPfx + a.Key,
Value: a.Value,
})
}
return c
}
// WithGroup returns a new [FormatHandler] that qualifies subsequent attributes
// with the given group name using dot-separated keys.
func (h *FormatHandler) WithGroup(name string) slog.Handler {
if name == "" {
return h
}
c := h.clone()
c.groups = append(c.groups, name)
c.grpPfx = strings.Join(c.groups, ".") + "."
return c
}
// clone creates a shallow copy of the handler, sharing the mutex and writer but
// with independent slices for preAttrs and groups.
func (h *FormatHandler) clone() *FormatHandler {
return &FormatHandler{
w: h.w,
level: h.level,
format: h.format,
timeLayout: h.timeLayout,
lineEnding: h.lineEnding,
preAttrs: slices.Clone(h.preAttrs),
groups: slices.Clone(h.groups),
grpPfx: h.grpPfx,
mu: h.mu, // shared across clones
needsSource: h.needsSource,
needsAttrs: h.needsAttrs,
needsLogger: h.needsLogger,
}
}
// formatAttrValue converts an [slog.Value] to its string representation.
// Group values are rendered as nested dot-separated key=value pairs.
func formatAttrValue(v slog.Value) string {
if v.Kind() == slog.KindGroup {
var sb strings.Builder
for i, a := range v.Group() {
if i > 0 {
sb.WriteByte(' ')
}
sb.WriteString(a.Key)
sb.WriteByte('=')
sb.WriteString(formatAttrValue(a.Value))
}
return sb.String()
}
return v.String()
}