-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
162 lines (145 loc) · 3.52 KB
/
log.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
// SPDX-FileCopyrightText: Fabio Forni <[email protected]>
// SPDX-License-Identifier: MPL-2.0
package main
import (
"bufio"
"context"
"fmt"
"io"
"log/slog"
"time"
"github.com/fatih/color"
)
// LogHandler is a slog handler with a focus on readability and aesthetics,
// although it sacrifices parsability a little.
type LogHandler struct {
dest *bufio.Writer
// group is a string idenfying a particular context while logging.
group string
// attribs is a collection of default attributes to be logged.
attribs []slog.Attr
opts LogHandlerOptions
}
func NewLogHandler(dest io.Writer, op *LogHandlerOptions) LogHandler {
var opts LogHandlerOptions
if op != nil {
opts = *op
} else {
opts = DefaultHandlerOptions()
}
return LogHandler{
dest: bufio.NewWriter(dest),
opts: opts,
}
}
// Enabled implements slog.Handler's Enabled function.
func (h LogHandler) Enabled(_ context.Context, level slog.Level) bool {
return level >= h.opts.Level
}
// Handle implements slog.Handler's Handle function.
func (h LogHandler) Handle(_ context.Context, rec slog.Record) error {
err := h.printPrefix(rec.Time)
if err != nil {
return err
}
err = h.printMessage(rec.Level, rec.Message)
if err != nil {
return err
}
err = h.printAttributes(rec)
if err != nil {
return err
}
err = h.dest.WriteByte('\n')
if err != nil {
return err
}
return h.dest.Flush()
}
// WithAttrs implements slog.Handler's WithAttrs function.
func (h LogHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
return LogHandler{
dest: h.dest,
group: h.group,
attribs: append(h.attribs, attrs...),
opts: h.opts,
}
}
// WithGroup implements slog.Handler's WithGroup function.
func (h LogHandler) WithGroup(group string) slog.Handler {
return LogHandler{
dest: h.dest,
group: group,
attribs: h.attribs,
opts: h.opts,
}
}
func (h LogHandler) printPrefix(t time.Time) error {
var err error
tim := t.Format("15:04:05")
if h.group != "" {
_, err = fmt.Fprintf(h.dest, "[%s] %s — ", tim, h.color(color.Bold).Sprint(h.group))
} else {
_, err = fmt.Fprintf(h.dest, "[%s] ", tim)
}
return err
}
func (h LogHandler) printMessage(level slog.Level, message string) error {
if message == "" {
return nil
}
var err error
if level == slog.LevelError {
_, err = h.color(color.FgRed).Fprintf(h.dest, "%s: %s", level, message)
} else {
_, err = fmt.Fprint(h.dest, message)
}
return err
}
func (h LogHandler) printAttributes(r slog.Record) error {
if r.NumAttrs() == 0 {
return nil
}
err := h.printSeparator()
if err != nil {
return err
}
r.Attrs(func(attr slog.Attr) bool {
if attr.Equal(slog.Attr{}) {
return true
}
_, err = fmt.Fprintf(h.dest, "%s: %s ", attr.Key, attr.Value)
return err == nil
})
return err
}
// printSeparator prints a separator between pieces of information.
func (h LogHandler) printSeparator() error {
_, err := fmt.Fprint(h.dest, " | ")
return err
}
func (h LogHandler) color(attrs ...color.Attribute) *color.Color {
var col *color.Color
if h.opts.Colored {
col = color.New(attrs...)
} else {
col = color.New()
col.DisableColor()
}
return col
}
// LogHandlerOptions sets the behavior of Handler.
type LogHandlerOptions struct {
// Level is the granularity of Handler.
Level slog.Level
// Colored specifies whether Handler will output ANSI colors.
// true actually means "auto", in that colors are disabled when
// the output is not a terminal.
Colored bool
}
func DefaultHandlerOptions() LogHandlerOptions {
return LogHandlerOptions{
Level: slog.LevelInfo,
Colored: true,
}
}