-
Notifications
You must be signed in to change notification settings - Fork 0
/
pretty_target.go
205 lines (182 loc) · 4.96 KB
/
pretty_target.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
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
package blackbox
import (
"fmt"
"io"
"sort"
"strings"
"time"
)
// NewPrettyTarget creates a PrettyTarget for use with a logger
func NewPrettyTarget(outTarget io.Writer, errTarget io.Writer) *PrettyTarget {
return &PrettyTarget{
outTarget: outTarget,
errTarget: errTarget,
level: Trace,
showTimestamp: true,
showLevel: true,
showContext: true,
useColor: true,
}
}
// 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 {
outTarget io.Writer
errTarget io.Writer
level Level
showTimestamp bool
showLevel bool
contextFields []string
showContext bool
useColor bool
}
// 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
}
// 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
}
// Log takes a Level and series of values, then outputs them formatted
// accordingly.
func (s *PrettyTarget) Log(level Level, values []interface{}, context Ctx) {
if level < s.level {
return
}
if s.showTimestamp {
s.writeCurrentTimestamp(level)
}
if s.showLevel {
s.writeLevel(level)
}
s.writeValues(level, values)
if s.showContext {
s.writeContext(level, context)
}
s.writeNewline(level)
}
func (s *PrettyTarget) writeCurrentTimestamp(level Level) {
timestampBytes := []byte(time.Now().Local().Format(time.RFC3339) + " ")
if level >= Warn {
s.errTarget.Write(timestampBytes)
} else {
s.outTarget.Write(timestampBytes)
}
}
func (s *PrettyTarget) writeLevel(level Level) {
levelStr := level.String()
var padStr string
for i := len(levelStr); i < 7; i++ {
padStr += " "
}
if s.useColor {
levelStr = wrapStrInAnsiLevelColorCodes(level, levelStr)
}
levelBytes := []byte(levelStr + padStr + " ")
if level >= Warn {
s.errTarget.Write(levelBytes)
} else {
s.outTarget.Write(levelBytes)
}
}
func (s *PrettyTarget) writeValues(level Level, values []interface{}) {
valueStrs := make([]string, 0)
for _, value := range values {
valueStrs = append(valueStrs, fmt.Sprintf("%+v", value))
}
messageBytes := []byte(strings.Join(valueStrs, " "))
if level >= Warn {
s.errTarget.Write(messageBytes)
} else {
s.outTarget.Write(messageBytes)
}
}
func (s *PrettyTarget) writeContext(level Level, context map[string]interface{}) {
contextStrs := make([]string, 0)
for key, value := range context {
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 = wrapStrInAnsiLevelColorCodes(level, key)
}
formattedValue := strings.Replace(fmt.Sprintf("%+v", value), "\n", "\\n", -1)
contextStrs = append(contextStrs, key+"="+formattedValue)
}
sort.Strings(contextStrs)
contextStr := strings.Join(contextStrs, " ")
contextBytes := []byte(" " + contextStr)
if level >= Warn {
s.errTarget.Write(contextBytes)
} else {
s.outTarget.Write(contextBytes)
}
}
func (s *PrettyTarget) writeNewline(level Level) {
newLineBytes := []byte("\n")
if level >= Warn {
s.errTarget.Write(newLineBytes)
} else {
s.outTarget.Write(newLineBytes)
}
}
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
}