-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
59 lines (49 loc) · 1.33 KB
/
logger.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
package boludo
import (
"context"
"io"
"log/slog"
"os"
)
// UnstructuredHandler is a slog.Handler that writes all log records to stderr
// without structure.
type UnstructuredHandler struct {
// Prefix specifies an optional prefix for each log record.
Prefix string
// Level specifies a minimum level for log records.
Level slog.Level
// Output specifies a destination for log records.
// If nil, os.Stderr is used.
Output io.Writer
}
// WithAttrs implements slog.Handler.
func (UnstructuredHandler) WithAttrs([]slog.Attr) slog.Handler {
return nil
}
// WithGroup implements slog.Handler.
func (UnstructuredHandler) WithGroup(string) slog.Handler { return nil }
// Enabled filters log records based on their level.
func (u UnstructuredHandler) Enabled(_ context.Context, l slog.Level) bool {
return l >= u.Level
}
// Handle writes log records to stderr.
func (u UnstructuredHandler) Handle(_ context.Context, r slog.Record) error {
output := u.Output
if output == nil {
output = os.Stderr
}
if u.Prefix != "" {
output.Write([]byte(u.Prefix))
output.Write([]byte{' '})
}
output.Write([]byte(r.Level.String()))
output.Write([]byte{' '})
output.Write([]byte(r.Message))
r.Attrs(func(a slog.Attr) bool {
output.Write([]byte{' '})
output.Write([]byte(a.String()))
return true
})
output.Write([]byte{'\n'})
return nil
}