-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathentry.go
More file actions
425 lines (381 loc) · 10.3 KB
/
Copy pathentry.go
File metadata and controls
425 lines (381 loc) · 10.3 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
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
// Package entryhuman contains the code to format slog.SinkEntry
// for humans.
package entryhuman
import (
"bytes"
"database/sql/driver"
"encoding/json"
"fmt"
"io"
"os"
"reflect"
"strconv"
"strings"
"syscall"
"time"
"unicode"
"github.com/charmbracelet/lipgloss"
"github.com/muesli/termenv"
"golang.org/x/term"
"golang.org/x/xerrors"
"cdr.dev/slog/v3"
)
// StripTimestamp strips the timestamp from entry and returns
// it and the rest of the entry.
func StripTimestamp(ent string) (time.Time, string, error) {
ts := ent[:len(TimeFormat)]
rest := ent[len(TimeFormat):]
et, err := time.Parse(TimeFormat, ts)
return et, rest, err
}
// TimeFormat is a simplified RFC3339 format.
const TimeFormat = "2006-01-02 15:04:05.000"
var (
renderer = lipgloss.NewRenderer(os.Stdout, termenv.WithUnsafe())
timeStyle = renderer.NewStyle().Foreground(lipgloss.Color("#606366"))
)
func render(w io.Writer, st lipgloss.Style, s string) string {
if shouldColor(w) {
ss := st.Render(s)
return ss
}
return s
}
func reset(w io.Writer, termW io.Writer) {
if shouldColor(termW) {
fmt.Fprintf(w, termenv.CSI+termenv.ResetSeq+"m")
}
}
func formatValue(v interface{}) (string, error) {
if vr, ok := v.(driver.Valuer); ok {
var err error
v, err = vr.Value()
if err != nil {
return "", xerrors.Errorf("error calling Value: %w", err)
}
}
if v == nil {
return "<nil>", nil
}
switch v.(type) {
case error, fmt.Stringer:
return quote(fmt.Sprintf("%+v", v)), nil
}
typ := reflect.TypeOf(v)
switch typ.Kind() {
case reflect.Struct, reflect.Map:
byt, err := json.Marshal(v)
if err != nil {
// don't panic
return "", xerrors.Errorf("error marshalling value: %w", err)
}
return string(byt), nil
case reflect.Slice:
// Byte slices are optimistically readable.
if typ.Elem().Kind() == reflect.Uint8 {
return fmt.Sprintf("%q", v), nil
}
fallthrough
default:
return quote(fmt.Sprintf("%+v", v)), nil
}
}
const tab = " "
// bracketedLevel is an optimization to avoid extra allocations and calls to strings.ToLower
// when we want to translate/print the lowercase version of a log level.
func bracketedLevel(l slog.Level) string {
switch l {
case slog.LevelDebug:
return "[debu]"
case slog.LevelInfo:
return "[info]"
case slog.LevelWarn:
return "[warn]"
case slog.LevelError:
return "[erro]"
case slog.LevelCritical:
return "[crit]"
case slog.LevelFatal:
return "[fata]"
default:
return "[unkn]"
}
}
func writeSignedInt(w io.Writer, n int64) (bool, error) {
var a [20]byte
_, err := w.Write(strconv.AppendInt(a[:0], n, 10))
return true, err
}
func writeUnsignedInt(w io.Writer, n uint64) (bool, error) {
var a [20]byte
_, err := w.Write(strconv.AppendUint(a[:0], n, 10))
return true, err
}
// Optimization to avoid allocation of heap allocations/temporary strings via formatValue when dealing with primitive types.
// It returns (handled, error). When handled is false, the caller should fall back to formatValue.
func writeValueFast(w io.Writer, v interface{}) (bool, error) {
switch x := v.(type) {
case string:
_, err := w.Write([]byte(quote(x)))
return true, err
case bool:
if x {
_, err := w.Write([]byte("true"))
return true, err
}
_, err := w.Write([]byte("false"))
return true, err
// signed ints
case int:
return writeSignedInt(w, int64(x))
case int8:
return writeSignedInt(w, int64(x))
case int16:
return writeSignedInt(w, int64(x))
case int32:
return writeSignedInt(w, int64(x))
case int64:
return writeSignedInt(w, x)
// unsigned ints
case uint:
return writeUnsignedInt(w, uint64(x))
case uint8:
return writeUnsignedInt(w, uint64(x))
case uint16:
return writeUnsignedInt(w, uint64(x))
case uint32:
return writeUnsignedInt(w, uint64(x))
case uint64:
return writeUnsignedInt(w, x)
// floats: prefer 'g' to keep output bounded (matches fmt default)
case float32:
var a [32]byte
_, err := w.Write(strconv.AppendFloat(a[:0], float64(x), 'g', -1, 32))
return true, err
case float64:
var a [32]byte
_, err := w.Write(strconv.AppendFloat(a[:0], x, 'g', -1, 64))
return true, err
default:
return false, nil
}
}
type Formatter struct {
ErrorCallback func(slog.Field, error)
}
// Fmt returns a human readable format for ent. Assumes we have a bytes.Buffer
// which we will more easily be able to assume underlying reallocation of it's size is possible
// if necessary than for an arbitrary io.Writer/io.StringWriter
// Note that while bytes.Buffer can in theory return an error for writes, it only does so if the buffer size will
// exceed our architectures max integer size. If the system is actually OOM and more memory cannot be allocated
// it will panic instead.
//
// We never return with a trailing newline because Go's testing framework adds one
// automatically and if we include one, then we'll get two newlines.
// We also do not indent the fields as go's test does that automatically
// for extra lines in a log so if we did it here, the fields would be indented
// twice in test logs. So the Stderr logger indents all the fields itself.
func Fmt(buf *bytes.Buffer, termW io.Writer, ent slog.SinkEntry) {
Formatter{}.Fmt(buf, termW, ent)
}
func (f Formatter) Fmt(buf *bytes.Buffer, termW io.Writer, ent slog.SinkEntry) {
reset(buf, termW)
// Timestamp + space
buf.WriteString(render(termW, timeStyle, ent.Time.Format(TimeFormat)))
buf.WriteString(" ")
// Level label + two spaces
lvl := bracketedLevel(ent.Level) // e.g. "[debu]", "[info]"
buf.WriteString(render(termW, levelStyle(ent.Level), lvl))
buf.WriteString(" ")
// Logger names: name1.name2.name3: (no strings.Join allocation)
if len(ent.LoggerNames) > 0 {
for i, name := range ent.LoggerNames {
if i > 0 {
buf.WriteString(".")
}
buf.WriteString(quoteKey(name))
}
buf.WriteString(": ")
}
// Message (detect multiline)
var multilineKey string
var multilineVal string
msg := strings.TrimSpace(ent.Message)
if strings.Contains(msg, "\n") {
multilineKey = "msg"
multilineVal = msg
msg = quote("...")
}
buf.WriteString(msg)
keyStyle := timeStyle
equalsStyle := timeStyle
// Write trace/span directly (do not mutate ent.Fields)
if ent.SpanContext.IsValid() {
buf.WriteString(tab)
buf.WriteString(render(termW, keyStyle, quoteKey("trace")))
buf.WriteString(render(termW, equalsStyle, "="))
buf.WriteString(ent.SpanContext.TraceID().String())
buf.WriteString(tab)
buf.WriteString(render(termW, keyStyle, quoteKey("span")))
buf.WriteString(render(termW, equalsStyle, "="))
buf.WriteString(ent.SpanContext.SpanID().String())
}
// Find a multiline field without mutating ent.Fields.
multiIdx := -1
for i, fld := range ent.Fields {
if multilineVal != "" {
break
}
var s string
switch v := fld.Value.(type) {
case string:
s = v
case error, xerrors.Formatter:
s = fmt.Sprintf("%+v", v)
}
s = strings.TrimSpace(s)
if !strings.Contains(s, "\n") {
continue
}
multiIdx = i
multilineKey = fld.Name
multilineVal = s
break
}
// Print fields (skip multiline field index).
for i, fld := range ent.Fields {
if i == multiIdx {
continue
}
if i < len(ent.Fields) {
buf.WriteString(tab)
}
buf.WriteString(render(termW, keyStyle, quoteKey(fld.Name)))
buf.WriteString(render(termW, equalsStyle, "="))
if ok, err := writeValueFast(buf, fld.Value); err != nil && f.ErrorCallback != nil {
f.ErrorCallback(fld, err)
} else if !ok {
s, err := formatValue(fld.Value)
if err != nil {
if f.ErrorCallback != nil {
f.ErrorCallback(fld, err)
}
buf.WriteString(err.Error())
continue
}
buf.WriteString(s)
}
}
// Multiline value block
if multilineVal != "" {
if msg != "..." {
buf.WriteString(" ...")
}
buf.WriteString("\n")
buf.WriteString(render(termW, keyStyle, multilineKey))
buf.WriteString("= ")
// First line up to first newline
s := multilineVal
if n := strings.IndexByte(s, '\n'); n >= 0 {
buf.WriteString(s[:n])
s = s[n+1:]
} else {
buf.WriteString(s)
s = ""
}
indent := strings.Repeat(" ", len(multilineKey)+2)
for len(s) > 0 {
buf.WriteString("\n")
// Only indent non-empty lines.
if s[0] != '\n' {
buf.WriteString(indent)
}
if n := strings.IndexByte(s, '\n'); n >= 0 {
buf.WriteString(s[:n])
s = s[n+1:]
} else {
buf.WriteString(s)
break
}
}
}
}
var (
levelDebugStyle = timeStyle.Copy()
levelInfoStyle = renderer.NewStyle().Foreground(lipgloss.Color("#0091FF"))
levelWarnStyle = renderer.NewStyle().Foreground(lipgloss.Color("#FFCF0D"))
levelErrorStyle = renderer.NewStyle().Foreground(lipgloss.Color("#FF5A0D"))
)
func levelStyle(level slog.Level) lipgloss.Style {
switch level {
case slog.LevelDebug:
return levelDebugStyle
case slog.LevelInfo:
return levelInfoStyle
case slog.LevelWarn:
return levelWarnStyle
case slog.LevelError, slog.LevelFatal, slog.LevelCritical:
return levelErrorStyle
default:
// don't panic
return levelErrorStyle
}
}
var forceColorWriter = io.Writer(&bytes.Buffer{})
// isTTY checks whether the given writer is a *os.File TTY.
func isTTY(w io.Writer) bool {
if w == forceColorWriter {
return true
}
// SyscallConn is safe during file close.
if sc, ok := w.(interface {
SyscallConn() (syscall.RawConn, error)
}); ok {
conn, err := sc.SyscallConn()
if err != nil {
return false
}
var isTerm bool
err = conn.Control(func(fd uintptr) {
isTerm = term.IsTerminal(int(fd))
})
if err != nil {
return false
}
return isTerm
}
// Fallback to unsafe Fd.
f, ok := w.(interface{ Fd() uintptr })
return ok && term.IsTerminal(int(f.Fd()))
}
func shouldColor(w io.Writer) bool {
return isTTY(w) || os.Getenv("FORCE_COLOR") != ""
}
// quotes quotes a string so that it is suitable
// as a key for a map or in general some output that
// cannot span multiple lines or have weird characters.
func quote(key string) string {
// strconv.Quote does not quote an empty string so we need this.
if key == "" {
return `""`
}
var hasSpace bool
for _, r := range key {
if unicode.IsSpace(r) {
hasSpace = true
break
}
}
quoted := strconv.Quote(key)
// If the key doesn't need to be quoted, don't quote it.
// We do not use strconv.CanBackquote because it doesn't
// account tabs.
if !hasSpace && quoted[1:len(quoted)-1] == key {
return key
}
return quoted
}
func quoteKey(key string) string {
// Replace spaces in the map keys with underscores.
return quote(strings.ReplaceAll(key, " ", "_"))
}