-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtext_handler.go
More file actions
189 lines (167 loc) · 4.1 KB
/
Copy pathtext_handler.go
File metadata and controls
189 lines (167 loc) · 4.1 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
package hyperfleetlogger
import (
"context"
"fmt"
"io"
"log/slog"
"strconv"
"strings"
"sync"
"time"
"unicode"
"unicode/utf8"
)
// textHandler formats log records as:
//
// {timestamp} {LEVEL} [{component}] [{version}] [{hostname}] {message} {key=value}...
type textHandler struct {
w io.Writer
mu *sync.Mutex
preformatted string
prefix string
component string
version string
hostname string
preStack []string
level slog.Level
sanitize bool
}
func newTextHandler(w io.Writer, level slog.Level, component, version, hostname string, sanitize bool) slog.Handler {
return &textHandler{
w: w,
level: level,
mu: &sync.Mutex{},
component: component,
version: version,
hostname: hostname,
sanitize: sanitize,
}
}
func (h *textHandler) Enabled(_ context.Context, level slog.Level) bool {
return level >= h.level
}
func (h *textHandler) Handle(_ context.Context, r slog.Record) error {
bp := bufPool.Get()
defer func() {
if cap(*bp) <= maxBufReuse {
*bp = (*bp)[:0]
bufPool.Put(bp)
}
}()
s := textState{buf: (*bp)[:0], stack: h.preStack}
if !r.Time.IsZero() {
s.buf = r.Time.AppendFormat(s.buf, time.RFC3339Nano)
s.buf = append(s.buf, ' ')
}
s.buf = append(s.buf, r.Level.String()...)
s.buf = append(s.buf, " ["...)
s.buf = h.appendText(s.buf, h.component)
s.buf = append(s.buf, "] ["...)
s.buf = h.appendText(s.buf, h.version)
s.buf = append(s.buf, "] ["...)
s.buf = h.appendText(s.buf, h.hostname)
s.buf = append(s.buf, "] "...)
s.buf = h.appendText(s.buf, r.Message)
s.buf = append(s.buf, h.preformatted...)
r.Attrs(func(a slog.Attr) bool {
h.appendAttr(&s, h.prefix, a)
return true
})
s.buf = append(s.buf, '\n')
if len(s.stack) > 0 {
s.buf = append(s.buf, " stack_trace:\n"...)
for _, frame := range s.stack {
s.buf = append(s.buf, " "...)
s.buf = h.appendText(s.buf, frame)
s.buf = append(s.buf, '\n')
}
}
*bp = s.buf
h.mu.Lock()
defer h.mu.Unlock()
if _, err := h.w.Write(s.buf); err != nil {
return fmt.Errorf("write text log record: %w", err)
}
return nil
}
func (h *textHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
if len(attrs) == 0 {
return h
}
h2 := *h
s := textState{buf: []byte(h.preformatted), stack: h.preStack}
for _, a := range attrs {
h.appendAttr(&s, h.prefix, a)
}
h2.preformatted, h2.preStack = string(s.buf), s.stack
return &h2
}
func (h *textHandler) WithGroup(name string) slog.Handler {
if name == "" {
return h
}
h2 := *h
h2.prefix = h.prefix + name + "."
return &h2
}
const maxBufReuse = 16 << 10
var bufPool = newPool(func() *[]byte { b := make([]byte, 0, 512); return &b })
type textState struct {
buf []byte
stack []string
}
func (h *textHandler) appendAttr(s *textState, prefix string, a slog.Attr) {
a.Value = a.Value.Resolve()
if a.Equal(slog.Attr{}) {
return
}
if a.Value.Kind() == slog.KindGroup {
if a.Key != "" {
prefix += a.Key + "."
}
for _, ga := range a.Value.Group() {
h.appendAttr(s, prefix, ga)
}
return
}
if a.Key == "" {
return
}
if prefix == "" && a.Key == FieldStackTrace {
if frames, ok := a.Value.Any().([]string); ok {
s.stack = frames
return
}
}
s.buf = append(s.buf, ' ')
s.buf = h.appendText(s.buf, prefix)
s.buf = h.appendText(s.buf, a.Key)
s.buf = append(s.buf, '=')
s.buf = h.appendTextValue(s.buf, a.Value)
}
func (h *textHandler) appendText(dst []byte, s string) []byte {
quote := strings.ContainsAny(s, "\n\r")
if !quote && h.sanitize {
quote = strings.ContainsFunc(s, func(r rune) bool { return r < 0x20 || r == 0x7f })
}
if quote {
return strconv.AppendQuote(dst, s)
}
return append(dst, s...)
}
func (h *textHandler) appendTextValue(dst []byte, v slog.Value) []byte {
if v.Kind() == slog.KindAny && v.Any() == nil {
return append(dst, "null"...)
}
str := v.String()
if needsQuoting(str) {
return strconv.AppendQuote(dst, str)
}
return append(dst, str...)
}
func needsQuoting(s string) bool {
return s == "" || strings.ContainsFunc(s, func(r rune) bool {
return r <= ' ' || r == '"' || r == '=' ||
r == utf8.RuneError || !unicode.IsPrint(r)
})
}