-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpretty_target.go
More file actions
279 lines (256 loc) · 7.13 KB
/
pretty_target.go
File metadata and controls
279 lines (256 loc) · 7.13 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package blackbox
import (
"fmt"
"io"
"os"
"path/filepath"
"sort"
"strings"
"time"
)
// PrettyTarget is a Target that produces newline separated human readable
// output suitable for stdout and stderr. It also supports colorized log levels.
type PrettyTarget struct {
showLoggerID bool
showTimestamp bool
showLevel bool
showContext bool
useColor bool
useSource bool
level Level
outTarget io.Writer
errTarget io.Writer
contextFields []string
}
var _ Target = &PrettyTarget{}
// NewPrettyTarget creates a PrettyTarget for use with a logger
func NewPrettyTarget(outTarget io.Writer, errTarget io.Writer) *PrettyTarget {
return &PrettyTarget{
showTimestamp: true,
showLevel: true,
showContext: true,
useColor: true,
level: Trace,
outTarget: outTarget,
errTarget: errTarget,
}
}
// SetLevel sets the minimum log level that PrettyTarget will output. Note that
// this setting is independent of the log level set on the logger itself.
func (s *PrettyTarget) SetLevel(level Level) *PrettyTarget {
s.level = level
return s
}
// ShowLoggerID will enable or disable logger ID values in the output depending
// on the boolean value passed. Logger IDs can be useful when the output of
// multiple loggers are viewed together.
func (s *PrettyTarget) ShowLoggerID(b bool) *PrettyTarget {
s.showLoggerID = b
return s
}
// ShowTimestamp will enable or disable timestamps in the output depending on
// the boolean value passed.
func (s *PrettyTarget) ShowTimestamp(b bool) *PrettyTarget {
s.showTimestamp = b
return s
}
// ShowLevel will enable or disable level labels in the output depending on
// the boolean value passed.
func (s *PrettyTarget) ShowLevel(b bool) *PrettyTarget {
s.showLevel = b
return s
}
// SelectContext will limit the context key value pairs in the output to only
// those specified as arguments to SelectContext. If select context is called
// no arguments then all context key value pairs will be output.
func (s *PrettyTarget) SelectContext(fields ...string) *PrettyTarget {
s.contextFields = fields
return s
}
// ShowContext will enable or disable context key value pairs in the output
// depending on the boolean value passed.
func (s *PrettyTarget) ShowContext(b bool) *PrettyTarget {
s.showContext = b
return s
}
// UseColor will enable or disable the use of ansi color codes in the output
// depending on the boolean value passed.
func (s *PrettyTarget) UseColor(b bool) *PrettyTarget {
s.useColor = b
return s
}
// ShowSource enables the inclusion of source
func (s *PrettyTarget) ShowSource(b bool) *PrettyTarget {
s.useSource = b
return s
}
// Log takes a Level and series of values, then outputs them formatted
// accordingly.
func (s *PrettyTarget) Log(loggerID string, level Level, values []any, context Ctx, getSource func() *Source) {
if level < s.level {
return
}
str := ""
if s.showLoggerID {
loggerIDStr := loggerID + " "
if s.useColor {
loggerIDStr = wrapStrInColorCodes("loggerID", loggerIDStr)
}
str += loggerIDStr
}
if s.showTimestamp {
timestampStr := time.Now().Local().Format("2006-01-02 15:04:05 MST") + " "
if s.useColor {
timestampStr = wrapStrInColorCodes("timestamp", timestampStr)
}
str += timestampStr
}
if s.showLevel {
levelStr := level.String()
var padStr string
for i := len(levelStr); i < 7; i++ {
padStr += " "
}
if s.useColor {
levelStr = wrapStrInAnsiLevelColorCodes(level, levelStr)
}
str += levelStr + padStr + " "
}
valueStrs := make([]string, 0)
for _, value := range values {
valueStrs = append(valueStrs, fmt.Sprintf("%+v", value))
}
valueStr := strings.Join(valueStrs, " ")
if s.useColor {
valueStr = wrapStrInColorCodes("value", valueStr)
}
str += valueStr
if s.showContext {
contextStrs := make([]string, 0)
for key, value := range context {
if strings.HasPrefix(key, "-") {
continue
}
if len(s.contextFields) != 0 {
skipField := true
for _, field := range s.contextFields {
if key == field {
skipField = false
break
}
}
if skipField {
continue
}
}
if s.useColor {
key = wrapStrInColorCodes("contextKey", key)
}
formattedValue := strings.Replace(fmt.Sprintf("%+v", value), "\n", "\\n", -1)
if s.useColor {
formattedValue = wrapStrInColorCodes("contextValue", formattedValue)
}
contextStrs = append(contextStrs, key+"="+formattedValue)
}
sort.Strings(contextStrs)
contextStr := strings.Join(contextStrs, " ")
str += " " + contextStr
}
if s.useSource {
source := getSource()
if source == nil {
return
}
functionAndPackageName := source.Function
funcPathChunks := strings.Split(functionAndPackageName, "/")
if len(funcPathChunks) > 0 {
functionAndPackageName = funcPathChunks[len(funcPathChunks)-1]
}
if s.useColor {
chunks := strings.Split(functionAndPackageName, ".")
colorizedChunks := make([]string, len(chunks))
for i, chunk := range chunks {
colorizedChunks[i] = wrapStrInColorCodes("packageAndFunctionName", chunk)
}
functionAndPackageName = strings.Join(colorizedChunks, ".")
}
filePath := source.File
cwd, getwdErr := os.Getwd()
if getwdErr == nil {
relFilePath, relErr := filepath.Rel(cwd, source.File)
if relErr == nil {
filePath = relFilePath
}
}
if s.useColor {
filePath = wrapStrInColorCodes("filePath", filePath)
}
lineNumber := fmt.Sprintf("%d", source.Line)
if s.useColor {
lineNumber = wrapStrInColorCodes("lineNumber", lineNumber)
}
separator := "@=>"
if s.useColor {
separator = wrapStrInColorCodes("separator", separator)
}
sourceStr := fmt.Sprintf(" %s %s:%s - %s", separator, filePath, lineNumber, functionAndPackageName)
str += sourceStr
}
str += "\n"
s.writeByLevel(level, str)
}
func (s *PrettyTarget) writeByLevel(level Level, str string) {
var err error
if level >= Warn {
_, err = s.errTarget.Write([]byte(str))
} else {
_, err = s.outTarget.Write([]byte(str))
}
if err != nil {
panic(err)
}
}
func wrapStrInAnsiLevelColorCodes(level Level, str string) string {
switch level {
case Trace:
str = "\u001b[35m" + str + "\u001b[0m"
case Debug:
str = "\u001b[34m" + str + "\u001b[0m"
case Verbose:
str = "\u001b[36m" + str + "\u001b[0m"
case Info:
str = "\u001b[32m" + str + "\u001b[0m"
case Warn:
str = "\u001b[33m" + str + "\u001b[0m"
case Error:
str = "\u001b[31m" + str + "\u001b[0m"
case Fatal:
str = "\u001b[37m\u001b[41;1m" + str + "\u001b[0m"
case Panic:
str = "\u001b[37m\u001b[45;1m" + str + "\u001b[0m"
}
return str
}
func wrapStrInColorCodes(kind string, str string) string {
switch kind {
case "loggerID":
return "\u001b[90;1m" + str + "\u001b[0m"
case "timestamp":
return "\u001b[90m" + str + "\u001b[0m"
case "value":
return "\u001b[37;1m" + str + "\u001b[0m"
case "separator":
return "\u001b[90m" + str + "\u001b[0m"
case "packageAndFunctionName":
return "\u001b[32m" + str + "\u001b[0m"
case "filePath":
return "\u001b[33m" + str + "\u001b[0m"
case "lineNumber":
return "\u001b[35m" + str + "\u001b[0m"
case "contextKey":
return "\u001b[90;1m" + str + "\u001b[0m"
case "contextValue":
return "\u001b[90m" + str + "\u001b[0m"
}
return str
}