-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
115 lines (100 loc) · 3.08 KB
/
common.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
package loggers
import (
"bytes"
"io"
"log/slog"
"os"
"path/filepath"
"runtime"
"strconv"
"time"
"go.elara.ws/loggers/internal/colors"
"golang.org/x/term"
)
// Options represents common options for slog handlers
// in this package.
type Options struct {
// TimeFormat represents the format of the timestamp
// provided by the [Pretty] logger.
TimeFormat string
// Level is the log level above which the handler will
// handle records.
Level slog.Level
// ShowCaller indicates whether the caller that created the
// record should be provided in the output
ShowCaller bool
// ForceColors prevents checking whether a handler outputs
// to a valid tty and always enables colors if set to true.
ForceColors bool
}
// groupOrAttr represents a group name or [log/slog.Attr].
type groupOrAttr struct {
attr slog.Attr
group string
}
// writeGroup writes all the attributes in a group to buf.
func writeGroup(colorize bool, buf *bytes.Buffer, group slog.Attr) {
attrs := group.Value.Group()
for i, attr := range attrs {
attr.Key = group.Key + "." + attr.Key
writeAttr(colorize, buf, attr)
if i < len(attrs)-1 {
buf.WriteByte(' ')
}
}
}
// writeAttr writes a single attribute to buf.
//
// If the attribute is a [log/slog.Group], it calls [writeGroup].
// If the value of the attribute is an error, it will color it red.
func writeAttr(colorize bool, buf *bytes.Buffer, attr slog.Attr) {
attr.Value = attr.Value.Resolve()
if attr.Equal(slog.Attr{}) {
return
}
if attr.Value.Kind() == slog.KindGroup {
writeGroup(colorize, buf, attr)
return
}
if _, ok := attr.Value.Any().(error); ok {
colors.WriteString(colorize, buf, colors.Red, attr.Key+"=")
colors.WriteCode(colorize, buf, colors.LightRed)
} else {
colors.WriteString(colorize, buf, colors.Cyan, attr.Key+"=")
colors.WriteCode(colorize, buf, colors.White)
}
abuf := buf.AvailableBuffer()
switch attr.Value.Kind() {
case slog.KindInt64:
abuf = strconv.AppendInt(abuf, attr.Value.Int64(), 10)
case slog.KindUint64:
abuf = strconv.AppendUint(abuf, attr.Value.Uint64(), 10)
case slog.KindFloat64:
abuf = strconv.AppendFloat(abuf, attr.Value.Float64(), 'g', -1, 64)
case slog.KindBool:
abuf = strconv.AppendBool(abuf, attr.Value.Bool())
case slog.KindDuration:
abuf = append(abuf, attr.Value.Duration().String()...)
case slog.KindTime:
abuf = attr.Value.Time().AppendFormat(abuf, time.RFC3339)
default:
abuf = strconv.AppendQuote(abuf, attr.Value.String())
}
buf.Write(abuf)
colors.WriteCode(colorize, buf, colors.Reset)
}
// writeCaller extracts the caller from the given program counter
// and writes it to buf, enclosed in square brackets.
func writeCaller(colorize bool, buf *bytes.Buffer, pc uintptr) {
frame, _ := runtime.CallersFrames([]uintptr{pc}).Next()
text := "[" + filepath.Base(frame.File) + ":" + strconv.Itoa(frame.Line) + "]"
buf.WriteByte(' ')
colors.WriteString(colorize, buf, colors.Bold+colors.LightBlue, text)
}
// isTerm checks if wr corresponds to a valid tty.
func isTerm(wr io.Writer) bool {
if fl, ok := wr.(*os.File); ok {
return term.IsTerminal(int(fl.Fd()))
}
return false
}