-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoder_console.go
More file actions
221 lines (202 loc) · 5 KB
/
encoder_console.go
File metadata and controls
221 lines (202 loc) · 5 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
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 blip
import (
"fmt"
"time"
)
// ConsoleEncoder is a console encoder that formats log messages in a
// human-readable format.
type ConsoleEncoder struct {
TimeFormat string
TimePrecision time.Duration
MinMessageWidth int
SortFields bool
Color bool
timeCache func(time.Time) string
}
const (
colorRed = "\033[31m"
colorGreen = "\033[32m"
colorYellow = "\033[33m"
colorBlue = "\033[34m"
colorPurple = "\033[35m"
colorCyan = "\033[36m"
colorOffWhite = "\033[37m"
colorRedBg = "\033[48;5;88m"
colorWhite = "\033[38;5;255m"
fontBold = "\033[1m"
fontReset = "\033[0m"
)
var _ Encoder = (*ConsoleEncoder)(nil)
// NewConsoleEncoder creates a new console encoder with the given configuration.
// The encoder formats log messages in a human-readable format, with
// colorized levels and optional field sorting.
// The encoder also supports a minimum message width for padding.
func NewConsoleEncoder() *ConsoleEncoder {
return &ConsoleEncoder{
TimeFormat: defaultTimeFormat,
TimePrecision: defaultTimePrecision,
MinMessageWidth: defaultMessageWidth,
SortFields: true,
Color: true,
}
}
// Start writes the beginning of the log message.
func (e *ConsoleEncoder) Start(_ *Buffer) {}
// EncodeTime encodes the time of the log message.
func (e *ConsoleEncoder) EncodeTime(buf *Buffer) {
if e.TimeFormat == "" {
return
}
if e.TimePrecision > 0 {
if e.timeCache == nil {
e.timeCache = timeCache(e.TimeFormat, e.TimePrecision)
}
buf.WriteString(e.timeCache(timeNow()))
} else {
buf.WriteTime(timeNow(), e.TimeFormat)
}
buf.WriteBytes(' ')
}
// EncodeLevel encodes the log level of the message.
func (e *ConsoleEncoder) EncodeLevel(buf *Buffer, lev Level) {
e.writeColorized(buf, lev, e.levelString(lev))
buf.WriteBytes(' ')
}
// EncodeMessage encodes the log message.
func (e *ConsoleEncoder) EncodeMessage(buf *Buffer, msg string) {
if e.Color {
buf.WriteString(fontBold)
}
buf.WriteString(msg)
if e.Color {
buf.WriteString(fontReset)
}
if e.MinMessageWidth == 0 {
return
}
// Pad the message to the configured width +2 spaces to separate it from
// the fields.
for range e.MinMessageWidth + 2 - len(msg) {
buf.WriteBytes(' ')
}
// If the message is long enough not to be padded, add an extra space to
// separate it from the fields
if len(msg) > e.MinMessageWidth {
// Separate message from fields with 2 spaces
buf.WriteBytes(' ', ' ')
}
}
// EncodeFields encodes the fields of the log message.
func (e *ConsoleEncoder) EncodeFields(buf *Buffer, lev Level, fields *[]Field) {
if fields == nil || len(*fields) == 0 {
return
}
if e.SortFields {
sortFields(*fields)
}
// Pad fields with two spaces
buf.WriteBytes(' ', ' ')
for i, f := range *fields {
if i > 0 {
buf.WriteBytes(' ')
}
e.writeColorized(buf, lev, f.Key)
buf.WriteBytes('=')
e.writeAny(buf, f.Value)
}
}
// EncodeStackTrace encodes the stack trace of the log message.
func (e *ConsoleEncoder) EncodeStackTrace(buf *Buffer, skip int) {
buf.WriteBytes('\n')
buf.WriteString(stackTrace(skip))
}
// End writes the end of the log message.
func (e *ConsoleEncoder) End(buf *Buffer) {
buf.WriteBytes('\n')
}
// WriteAny writes a value of any type to the buffer. It handles various types
// and falls back to fmt.Sprint for unsupported types.
//
//nolint:gocyclo
func (e *ConsoleEncoder) writeAny(buf *Buffer, val any) {
switch v := val.(type) {
case string:
buf.WriteString(v)
case []byte:
buf.WriteBytes(v...)
case int:
buf.WriteInt(int64(v))
case int8:
buf.WriteInt(int64(v))
case int16:
buf.WriteInt(int64(v))
case int32:
buf.WriteInt(int64(v))
case int64:
buf.WriteInt(v)
case uint:
buf.WriteUint(uint64(v))
case uint8:
buf.WriteUint(uint64(v))
case uint16:
buf.WriteUint(uint64(v))
case uint32:
buf.WriteUint(uint64(v))
case uint64:
buf.WriteUint(v)
case float32:
buf.WriteFloat(float64(v), 32)
case float64:
buf.WriteFloat(v, 64)
case bool:
buf.WriteBool(v)
case time.Duration:
buf.WriteDuration(v.Truncate(DurationFieldPrecision))
case time.Time:
buf.WriteTime(v, TimeFieldFormat)
default:
// TODO: Add support for custom encoders
buf.WriteString(fmt.Sprint(v))
}
}
func (e *ConsoleEncoder) writeColorized(buf *Buffer, lev Level, str string) {
if !e.Color {
buf.WriteString(str)
return
}
switch lev {
case LevelTrace, LevelDebug:
buf.WriteString(colorOffWhite)
case LevelInfo:
buf.WriteString(colorCyan)
case LevelWarn:
buf.WriteString(colorYellow)
case LevelError:
buf.WriteString(colorRed)
case LevelPanic, LevelFatal:
buf.WriteString(colorRedBg)
buf.WriteString(colorWhite)
}
buf.WriteString(str)
buf.WriteString(fontReset)
}
func (e *ConsoleEncoder) levelString(lev Level) string {
switch lev {
case LevelTrace:
return "TRAC"
case LevelDebug:
return "DEBU"
case LevelInfo:
return "INFO"
case LevelWarn:
return "WARN"
case LevelError:
return "ERRO"
case LevelPanic:
return "PANI"
case LevelFatal:
return "FATA"
default:
panic("unreachable")
}
}