-
Notifications
You must be signed in to change notification settings - Fork 0
/
tint.go
400 lines (342 loc) · 9.1 KB
/
tint.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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
// File: "tint.go"
// Tinted (colorized) logger based on "github.com/lmittmann/tint" sources
package xlog
import (
"context"
"encoding"
"fmt"
"io"
"log/slog" // go>=1.21
"path"
"path/filepath"
"runtime"
"strconv"
"sync"
"time"
"unicode"
//"golang.org/x/exp/slog" // depricated for go>=1.21
)
// Time formats
const (
// Time OFF
TIME_OFF = ""
// Default time format of standart logger
STD_TIME = "2006/01/02 15:04:05"
// Default time format of standart logger + microseconds
STD_TIME_US = "2006/01/02 15:04:05.999999"
// Default time format of standart logger + milliseconds
STD_TIME_MS = "2006/01/02 15:04:05.999"
// RFC3339 time format + nanoseconds (slog.TextHandler by default)
RFC3339Nano = time.RFC3339Nano // "2006-01-02T15:04:05.999999999Z07:00"
// RFC3339 time format + microseconds
RFC3339Micro = "2006-01-02T15:04:05.999999Z07:00"
// RFC3339 time format + milliseconds
RFC3339Milli = "2006-01-02T15:04:05.999Z07:00"
// Time only format + microseconds
TimeOnlyMicro = "15:04:05.999999"
// Time only format + milliseconds
TimeOnlyMilli = "15:04:05.999"
// Default (recomented) time format wuth milliseconds
DEFAULT_TIME_FORMAT = STD_TIME_MS
// Default (recomented) time format witn microseconds
DEFAULT_TIME_FORMAT_US = STD_TIME_US
)
type TintOptions struct {
// Enable source code location
AddSource bool
// Log long file path (directory + file name)
SourceLong bool
// Minimum level to log (Default: slog.LevelInfo)
Level slog.Leveler
// Off level keys
NoLevel bool
// ReplaceAttr is called to rewrite each non-group attribute before it is logged.
// See https://pkg.go.dev/log/slog#HandlerOptions for details.
ReplaceAttr func(groups []string, attr slog.Attr) slog.Attr
// Time format
TimeFormat string
// Disable color
NoColor bool
}
// Tinted (colorized) handler implements a slog.Handler
type TintHandler struct {
attrsPrefix string
groupPrefix string
groups []string
mu sync.Mutex
w io.Writer
addSource bool
sourceLong bool
level slog.Leveler
noLevel bool
replaceAttr func([]string, slog.Attr) slog.Attr
timeFormat string
noColor bool
}
// Create new tinted (colorized) handler
func NewTintHandler(w io.Writer, opts *TintOptions) *TintHandler {
h := &TintHandler{
w: w,
level: DEFAULT_LEVEL,
timeFormat: TIME_OFF,
}
if opts == nil {
return h
}
h.addSource = opts.AddSource
h.sourceLong = opts.SourceLong
h.noLevel = opts.NoLevel
h.noColor = opts.NoColor
h.replaceAttr = opts.ReplaceAttr
if opts.Level != nil {
h.level = opts.Level
}
if opts.TimeFormat != "" {
h.timeFormat = opts.TimeFormat
}
return h
}
// Enabled() implements slog.Handler interface
func (h *TintHandler) Enabled(_ context.Context, level slog.Level) bool {
return level >= h.level.Level()
}
// Handle() implements slog.Handler interface
func (h *TintHandler) Handle(_ context.Context, r slog.Record) error {
// Get a buffer from the sync pool
buf := NewBuffer()
defer buf.Free()
rep := h.replaceAttr
// Write time
if h.timeFormat != TIME_OFF && !r.Time.IsZero() {
val := r.Time.Round(0) // strip monotonic to match Attr behavior
if rep == nil {
h.appendTime(buf, r.Time)
buf.WriteByte(' ')
} else if a := rep(nil /* groups */, slog.Time(slog.TimeKey, val)); a.Key != "" {
if a.Value.Kind() == slog.KindTime {
h.appendTime(buf, a.Value.Time())
} else {
h.appendValue(buf, a.Value, false)
}
buf.WriteByte(' ')
}
}
// Write level
if !h.noLevel {
if rep == nil {
h.appendLevel(buf, r.Level)
buf.WriteByte(' ')
} else if a := rep(nil /* groups */, slog.Any(slog.LevelKey, r.Level)); a.Key != "" {
h.appendValue(buf, a.Value, false)
buf.WriteByte(' ')
}
}
// Write source
if h.addSource {
fs := runtime.CallersFrames([]uintptr{r.PC})
f, _ := fs.Next()
if f.File != "" {
src := &slog.Source{
Function: f.Function,
File: f.File,
Line: f.Line,
}
if !h.sourceLong {
src.File = path.Base(src.File) // only file name
}
if rep == nil {
h.appendSource(buf, src)
buf.WriteByte(' ')
} else if a := rep(nil /* groups */, slog.Any(slog.SourceKey, src)); a.Key != "" {
h.appendValue(buf, a.Value, false)
buf.WriteByte(' ')
}
}
}
// Write message
if rep == nil {
buf.WriteString(r.Message)
buf.WriteByte(' ')
} else if a := rep(nil /* groups */, slog.String(slog.MessageKey, r.Message)); a.Key != "" {
h.appendValue(buf, a.Value, false)
buf.WriteByte(' ')
}
// Write handler attributes
if len(h.attrsPrefix) > 0 {
buf.WriteString(h.attrsPrefix)
}
// Write attributes
r.Attrs(func(attr slog.Attr) bool {
h.appendAttr(buf, attr, h.groupPrefix, h.groups)
return true
})
if len(*buf) == 0 {
return nil
}
(*buf)[len(*buf)-1] = '\n' // replace last space with newline
h.mu.Lock()
defer h.mu.Unlock()
_, err := h.w.Write(*buf)
return err
}
// WithAttrs() implements slog.Handler interface
func (h *TintHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
if len(attrs) == 0 {
return h
}
h2 := h.clone()
buf := NewBuffer()
defer buf.Free()
// write attributes to buffer
for _, attr := range attrs {
h.appendAttr(buf, attr, h.groupPrefix, h.groups)
}
h2.attrsPrefix = h.attrsPrefix + string(*buf)
return h2
}
// WithGroup() implements slog.Handler interface
func (h *TintHandler) WithGroup(name string) slog.Handler {
if name == "" {
return h
}
h2 := h.clone()
h2.groupPrefix += name + "."
h2.groups = append(h2.groups, name)
return h2
}
func (h *TintHandler) clone() *TintHandler {
return &TintHandler{
attrsPrefix: h.attrsPrefix,
groupPrefix: h.groupPrefix,
groups: h.groups,
w: h.w,
addSource: h.addSource,
level: h.level,
replaceAttr: h.replaceAttr,
timeFormat: h.timeFormat,
noColor: h.noColor,
}
}
func (h *TintHandler) appendTime(buf *Buffer, t time.Time) {
if h.noColor {
*buf = t.AppendFormat(*buf, h.timeFormat)
} else {
buf.WriteString(AnsiTime)
*buf = t.AppendFormat(*buf, h.timeFormat)
buf.WriteString(AnsiReset)
}
}
func (h *TintHandler) appendLevel(buf *Buffer, level slog.Level) {
xl := Level(level)
if h.noColor {
buf.WriteString(xl.String())
} else {
buf.WriteString(xl.ColorString())
}
}
func (h *TintHandler) appendSource(buf *Buffer, src *slog.Source) {
if !h.noColor {
buf.WriteString(AnsiSource)
defer buf.WriteString(AnsiReset)
}
dir, file := filepath.Split(src.File)
buf.WriteString(filepath.Join(filepath.Base(dir), file))
buf.WriteByte(':')
buf.WriteString(strconv.Itoa(src.Line))
}
func (h *TintHandler) appendAttr(buf *Buffer, attr slog.Attr,
groupsPrefix string, groups []string) {
attr.Value = attr.Value.Resolve()
if rep := h.replaceAttr; rep != nil && attr.Value.Kind() != slog.KindGroup {
attr = rep(groups, attr)
attr.Value = attr.Value.Resolve()
}
if attr.Equal(slog.Attr{}) {
return
}
if attr.Value.Kind() == slog.KindGroup {
if attr.Key != "" {
groupsPrefix += attr.Key + "."
groups = append(groups, attr.Key)
}
for _, groupAttr := range attr.Value.Group() {
h.appendAttr(buf, groupAttr, groupsPrefix, groups)
}
} else if err, ok := attr.Value.Any().(error); err != nil && ok {
// appen error
h.appendError(buf, attr.Key, err, groupsPrefix)
buf.WriteByte(' ')
} else {
h.appendKey(buf, attr.Key, groupsPrefix)
h.appendValue(buf, attr.Value, true)
buf.WriteByte(' ')
}
}
func (h *TintHandler) appendKey(buf *Buffer, key, groups string) {
if !h.noColor {
buf.WriteString(AnsiKey)
defer buf.WriteString(AnsiReset)
}
appendString(buf, groups+key, true)
buf.WriteByte('=')
}
func (h *TintHandler) appendValue(buf *Buffer, v slog.Value, quote bool) {
switch v.Kind() {
case slog.KindString:
appendString(buf, v.String(), quote)
case slog.KindInt64:
*buf = strconv.AppendInt(*buf, v.Int64(), 10)
case slog.KindUint64:
*buf = strconv.AppendUint(*buf, v.Uint64(), 10)
case slog.KindFloat64:
*buf = strconv.AppendFloat(*buf, v.Float64(), 'g', -1, 64)
case slog.KindBool:
*buf = strconv.AppendBool(*buf, v.Bool())
case slog.KindDuration:
appendString(buf, v.Duration().String(), quote)
case slog.KindTime:
appendString(buf, v.Time().String(), quote)
case slog.KindAny:
switch cv := v.Any().(type) {
case slog.Level:
h.appendLevel(buf, cv)
case encoding.TextMarshaler:
data, err := cv.MarshalText()
if err != nil {
break
}
appendString(buf, string(data), quote)
case *slog.Source:
h.appendSource(buf, cv)
default:
appendString(buf, fmt.Sprint(v.Any()), quote)
}
}
}
func (h *TintHandler) appendError(buf *Buffer, key string, err error, groupsPrefix string) {
buf.WriteStringIf(!h.noColor, AnsiErrKey)
appendString(buf, groupsPrefix+key, true)
buf.WriteByte('=')
buf.WriteStringIf(!h.noColor, AnsiErrVal)
appendString(buf, err.Error(), true)
buf.WriteStringIf(!h.noColor, AnsiReset)
}
func appendString(buf *Buffer, s string, quote bool) {
if quote && needsQuoting(s) {
*buf = strconv.AppendQuote(*buf, s)
} else {
buf.WriteString(s)
}
}
func needsQuoting(s string) bool {
if len(s) == 0 {
return true
}
for _, r := range s {
if unicode.IsSpace(r) || r == '"' || r == '=' || !unicode.IsPrint(r) {
return true
}
}
return false
}
// EOF: "tint.go"