-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcli_handler.go
More file actions
166 lines (135 loc) · 3.41 KB
/
cli_handler.go
File metadata and controls
166 lines (135 loc) · 3.41 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
package slogx
import (
"context"
"fmt"
"github.com/fatih/color"
"github.com/ttys3/slogx/internal"
"io"
"log/slog"
"sync"
)
var bold = color.New(color.Bold)
func init() {
bold.EnableColor()
}
// Colors mapping.
var Colors = map[slog.Level]*color.Color{
slog.LevelDebug: color.New(color.FgWhite),
slog.LevelInfo: color.New(color.FgBlue),
slog.LevelWarn: color.New(color.FgYellow),
slog.LevelError: color.New(color.FgRed),
}
// Strings mapping.
var Strings = map[slog.Level]string{
slog.LevelDebug: "•",
slog.LevelInfo: "•",
slog.LevelWarn: "•",
slog.LevelError: "⨯",
}
type CliHandler struct {
mu sync.Mutex
w io.Writer
opts *CliHandlerOptions
attrsPrefix []slog.Attr
groupPrefix string
}
type CliHandlerOptions struct {
DisableColor bool
slog.HandlerOptions
}
func NewCliHandler(w io.Writer, opts *CliHandlerOptions) *CliHandler {
return &CliHandler{w: w, opts: opts}
}
func (h *CliHandler) Enabled(ctx context.Context, l slog.Level) bool {
minLevel := slog.LevelInfo
if h.opts.Level != nil {
minLevel = h.opts.Level.Level()
}
return l >= minLevel
}
func (h *CliHandler) Handle(ctx context.Context, r slog.Record) error {
// level time message attributes// get a buffer from the sync pool
// get a buffer from the sync pool
buf := internal.NewBuffer()
defer buf.Free()
theColor := Colors[r.Level]
if h.opts.DisableColor {
theColor.DisableColor()
} else {
theColor.EnableColor()
}
levelEmoji := Strings[r.Level]
padding := 4
coloredLevel := theColor.Sprintf("%s", bold.Sprintf("%*s", padding, levelEmoji))
buf.WriteString(coloredLevel)
buf.WriteString(" ")
buf.WriteString(fmt.Sprintf("%-25s", r.Message))
buf.WriteString("\t\t")
// write handler attributes
if len(h.attrsPrefix) > 0 {
for _, attr := range h.attrsPrefix {
h.appendAttr(buf, attr, theColor, h.groupPrefix)
}
}
// write attributes
if r.NumAttrs() > 0 {
r.Attrs(func(attr slog.Attr) bool {
h.appendAttr(buf, attr, theColor, h.groupPrefix)
return true
})
}
buf.WriteByte('\n')
h.mu.Lock()
defer h.mu.Unlock()
_, err := h.w.Write(buf.Bytes())
if err != nil {
return err
}
return nil
}
func (h *CliHandler) appendAttr(buf *internal.Buffer, attr slog.Attr, theColor *color.Color, groupsPrefix string) {
buf.Write([]byte(" "))
if groupsPrefix != "" {
buf.WriteString(theColor.Sprint(groupsPrefix))
}
buf.WriteString(theColor.Sprint(attr.Key))
buf.Write([]byte("="))
// needQuote := attr.Value.Kind() != slog.KindInt64
// if needQuote {
// buf.Write([]byte(`"`))
// }
if attr.Value.Kind() != slog.KindGroup {
buf.Write([]byte(attr.Value.String()))
} else {
buf.Write([]byte("{"))
for _, attr := range attr.Value.Group() {
h.appendAttr(buf, attr, theColor, groupsPrefix)
}
buf.Write([]byte(" }"))
}
// if needQuote {
// buf.Write([]byte(`"`))
// }
}
func (h *CliHandler) clone() *CliHandler {
attrsPrefix := make([]slog.Attr, len(h.attrsPrefix))
copy(attrsPrefix, h.attrsPrefix)
return &CliHandler{w: h.w, opts: h.opts, attrsPrefix: attrsPrefix, groupPrefix: h.groupPrefix}
}
func (h *CliHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
if len(attrs) == 0 {
return h
}
cloned := h.clone()
cloned.attrsPrefix = append(cloned.attrsPrefix, attrs...)
return cloned
}
func (h *CliHandler) WithGroup(name string) slog.Handler {
if name == "" {
return h
}
cloned := h.clone()
cloned.groupPrefix += name + "."
return cloned
}
var _ slog.Handler = (*CliHandler)(nil)