forked from antonfisher/nested-logrus-formatter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
formatter.go
220 lines (177 loc) · 4.45 KB
/
formatter.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package formatter
import (
"bytes"
"fmt"
"runtime"
"sort"
"strings"
"time"
"github.com/sirupsen/logrus"
)
// Formatter - logrus formatter, implements logrus.Formatter
type Formatter struct {
// FieldsOrder - default: fields sorted alphabetically
FieldsOrder []string
// TimestampFormat - default: time.StampMilli = "Jan _2 15:04:05.000"
TimestampFormat string
// HideKeys - show [fieldValue] instead of [fieldKey:fieldValue]
HideKeys bool
// HidePartialKeys - show [fieldValue] instead of [fieldKey:fieldValue] if fieldKey in the map
// HidePartialKeys will have a lower priority than HideKeys.
HidePartialKeys map[string]bool
// NoColors - disable colors
NoColors bool
// NoFieldsColors - apply colors only to the level, default is level + fields
NoFieldsColors bool
// NoFieldsSpace - no space between fields
NoFieldsSpace bool
// ShowFullLevel - show a full level [WARNING] instead of [WARN]
ShowFullLevel bool
// NoUppercaseLevel - no upper case for level value
NoUppercaseLevel bool
// TrimMessages - trim whitespaces on messages
TrimMessages bool
// CallerFirst - print caller info first
CallerFirst bool
// CustomCallerFormatter - set custom formatter for caller info
CustomCallerFormatter func(*runtime.Frame) string
}
// Format an log entry
func (f *Formatter) Format(entry *logrus.Entry) ([]byte, error) {
levelColor := getColorByLevel(entry.Level)
timestampFormat := f.TimestampFormat
if timestampFormat == "" {
timestampFormat = time.StampMilli
}
// output buffer
b := &bytes.Buffer{}
// write time
b.WriteString(entry.Time.Format(timestampFormat))
// write level
var level string
if f.NoUppercaseLevel {
level = entry.Level.String()
} else {
level = strings.ToUpper(entry.Level.String())
}
if f.CallerFirst {
f.writeCaller(b, entry)
}
if !f.NoColors {
fmt.Fprintf(b, "\x1b[%dm", levelColor)
}
b.WriteString(" [")
if f.ShowFullLevel {
b.WriteString(level)
} else {
b.WriteString(level[:4])
}
b.WriteString("]")
if !f.NoFieldsSpace {
b.WriteString(" ")
}
if !f.NoColors && f.NoFieldsColors {
b.WriteString("\x1b[0m")
}
// write fields
if f.FieldsOrder == nil {
f.writeFields(b, entry)
} else {
f.writeOrderedFields(b, entry)
}
if f.NoFieldsSpace {
b.WriteString(" ")
}
if !f.NoColors && !f.NoFieldsColors {
b.WriteString("\x1b[0m")
}
// write message
if f.TrimMessages {
b.WriteString(strings.TrimSpace(entry.Message))
} else {
b.WriteString(entry.Message)
}
if !f.CallerFirst {
f.writeCaller(b, entry)
}
b.WriteByte('\n')
return b.Bytes(), nil
}
func (f *Formatter) writeCaller(b *bytes.Buffer, entry *logrus.Entry) {
if entry.HasCaller() {
if f.CustomCallerFormatter != nil {
fmt.Fprintf(b, f.CustomCallerFormatter(entry.Caller))
} else {
fmt.Fprintf(
b,
" (%s:%d %s)",
entry.Caller.File,
entry.Caller.Line,
entry.Caller.Function,
)
}
}
}
func (f *Formatter) writeFields(b *bytes.Buffer, entry *logrus.Entry) {
if len(entry.Data) != 0 {
fields := make([]string, 0, len(entry.Data))
for field := range entry.Data {
fields = append(fields, field)
}
sort.Strings(fields)
for _, field := range fields {
f.writeField(b, entry, field)
}
}
}
func (f *Formatter) writeOrderedFields(b *bytes.Buffer, entry *logrus.Entry) {
length := len(entry.Data)
foundFieldsMap := map[string]bool{}
for _, field := range f.FieldsOrder {
if _, ok := entry.Data[field]; ok {
foundFieldsMap[field] = true
length--
f.writeField(b, entry, field)
}
}
if length > 0 {
notFoundFields := make([]string, 0, length)
for field := range entry.Data {
if foundFieldsMap[field] == false {
notFoundFields = append(notFoundFields, field)
}
}
sort.Strings(notFoundFields)
for _, field := range notFoundFields {
f.writeField(b, entry, field)
}
}
}
func (f *Formatter) writeField(b *bytes.Buffer, entry *logrus.Entry, field string) {
if f.HideKeys || (f.HidePartialKeys != nil && f.HidePartialKeys[field]) {
fmt.Fprintf(b, "[%v]", entry.Data[field])
} else {
fmt.Fprintf(b, "[%s:%v]", field, entry.Data[field])
}
if !f.NoFieldsSpace {
b.WriteString(" ")
}
}
const (
colorRed = 31
colorYellow = 33
colorBlue = 36
colorGray = 37
)
func getColorByLevel(level logrus.Level) int {
switch level {
case logrus.DebugLevel, logrus.TraceLevel:
return colorGray
case logrus.WarnLevel:
return colorYellow
case logrus.ErrorLevel, logrus.FatalLevel, logrus.PanicLevel:
return colorRed
default:
return colorBlue
}
}