-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
221 lines (172 loc) · 4.24 KB
/
handler.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
package slug
import (
"context"
"fmt"
"io"
"log/slog"
"runtime"
"strings"
"sync"
"github.com/dotse/slug/internal"
"github.com/logrusorgru/aurora/v4"
"golang.org/x/text/language"
"golang.org/x/text/message"
)
const (
// DefaultTimeFormat is the default format for any [time.Time] logged.
DefaultTimeFormat = "2006-01-02T15:04:05,000Z07:00"
)
var (
_ slog.Handler = (*Handler)(nil)
_ slog.Leveler = (*Handler)(nil)
)
// Handler is a [slog.Handler] that writes human-readable logs.
type Handler struct {
shared *shared
attrs []slog.Attr
groups []string
}
// NewHandler returns a new [Handler] with [HandlerOptions].
func NewHandler(options HandlerOptions, w io.Writer) *Handler {
if options.Level == nil {
options.Level = slog.LevelInfo
}
if options.Language == language.Und {
options.Language = language.BritishEnglish
}
if options.TimeFormat == "" {
options.TimeFormat = DefaultTimeFormat
}
return &Handler{
shared: &shared{
Writer: w,
options: options,
printer: message.NewPrinter(options.Language),
},
attrs: []slog.Attr{},
groups: []string{},
}
}
// Enabled returns true if a message at a [slog.Level] would be logged.
func (h *Handler) Enabled(_ context.Context, level slog.Level) bool {
return level >= h.Level()
}
// Handle handles a [slog.Record].
func (h *Handler) Handle(_ context.Context, r slog.Record) error {
t := aurora.Faint(r.Time.Format(h.shared.options.TimeFormat))
l := aurora.Bold(r.Level.String())
switch {
case r.Level >= slog.LevelError:
l = l.Red()
case r.Level >= slog.LevelWarn:
l = l.Yellow()
case r.Level <= slog.LevelDebug:
l = l.Faint()
default:
l = l.Blue()
}
h.shared.Lock()
defer h.shared.Unlock()
fmt.Fprintf(h.shared, "%s %-5s %s", t, l, internal.Escape(r.Message))
prefix := strings.Join(append(h.groups, ""), ".")
for _, attr := range h.attrs {
h.printAttr(prefix, attr)
}
r.Attrs(func(attr slog.Attr) bool {
h.printAttr(prefix, attr)
return true
})
if h.shared.options.AddSource && r.PC != 0 {
frame, _ := runtime.CallersFrames([]uintptr{r.PC}).Next()
h.printAttr("", slog.String("file", frame.File))
h.printAttr("", slog.Int("line", frame.Line))
h.printAttr("", slog.String("function", frame.Function))
}
fmt.Fprintln(h.shared)
return nil
}
// Level returns the current [slog.Level].
func (h *Handler) Level() slog.Level {
return h.shared.options.Level.Level()
}
// WithAttrs returns a hew [Handler] with additional attributes.
func (h *Handler) WithAttrs(attrs []slog.Attr) slog.Handler {
if len(attrs) == 0 {
return h
}
c := h.clone()
c.attrs = append(c.attrs, attrs...)
return c
}
// WithGroup returns a hew [Handler] with an additional group.
func (h *Handler) WithGroup(name string) slog.Handler {
if name == "" {
return h
}
c := h.clone()
c.groups = append(c.groups, name)
return c
}
func (h *Handler) clone() *Handler {
return &Handler{
shared: h.shared,
attrs: h.attrs,
groups: h.groups,
}
}
func (h *Handler) printAttr(prefix string, attr slog.Attr) {
value := attr.Value.Resolve()
var (
isErr bool
val string
)
switch value.Kind() {
case slog.KindFloat64:
val = h.shared.printer.Sprint(value.Float64())
case slog.KindGroup:
prefix += attr.Key + "."
for _, attr := range value.Group() {
h.printAttr(prefix, attr)
}
return
case slog.KindInt64:
val = h.shared.printer.Sprint(value.Int64())
case slog.KindTime:
val = value.Time().Format(h.shared.options.TimeFormat)
case slog.KindUint64:
val = h.shared.printer.Sprint(value.Uint64())
case slog.KindAny:
_, isErr = value.Any().(error)
fallthrough
default:
val = internal.Escape(value.String())
}
var (
k = aurora.Bold(attr.Key)
v aurora.Value
)
if isErr {
k = k.Red()
v = aurora.Red(val)
} else {
k = k.Magenta()
v = aurora.Cyan(val)
}
fmt.Fprintf(h.shared, " %s%s=%s", aurora.Magenta(prefix), k, v)
}
// HandlerOptions are options for a [Handler].
type HandlerOptions struct {
// Use all of [slog.HandlerOptions].
slog.HandlerOptions
// Language for formatting numbers.
Language language.Tag
// TimeFormat for timestamps and [time.Time] attributes. Defaults to
// [DefaultTimeFormat] if unset.
TimeFormat string
}
type shared struct {
io.Writer
options HandlerOptions
printer *message.Printer
sync.Mutex
}