-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtext_formatter.go
187 lines (162 loc) · 4.99 KB
/
text_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
package log
import (
"bytes"
"fmt"
"strings"
"sync"
"text/template"
"github.com/bdlm/errors/v2"
)
var (
termTemplate = template.Must(template.New("tty").Parse(
"{{$color := .Color}}{{$caller := .Caller}}" +
// Level
"{{$color.Level}}{{printf \"%5s\" .Level}}{{$color.Reset}}" +
// Hostname
"{{if .Hostname}} {{$color.Hostname}}{{.Hostname}}{{$color.Reset}}{{end}} " +
// Message
"{{printf \"%s\" .Message}}" +
// Data fields
"{{if .Data}}\n {{$color.Level}}⇢{{$color.Reset}} {{range $k, $v := .Data}}" +
" {{$color.DataLabel}}{{$k}}{{$color.Reset}}={{$color.DataValue}}{{$v}}{{$color.Reset}}" +
"{{end}}{{end}}" +
// Caller
"{{if and (.Caller) (not .Trace)}}" +
"\n {{$color.Level}}⇢{{$color.Reset}} {{$color.Caller}}{{.Caller}}{{$color.Reset}}" +
"{{end}}" +
// Error
"{{if .Err}}{{range $k, $v := .ErrData}}" +
"\n {{$color.Level}}⇢{{$color.Reset}} {{$color.Err}}#{{$k}}: {{$v}}{{$color.Reset}}" +
"{{end}}{{end}}" +
// Trace
"{{range $k, $v := .Trace}}" +
"\n {{$color.Level}}⇢{{$color.Reset}} {{$color.Trace}}#{{$k}} {{$v}}{{$color.Reset}}" +
"{{end}}" +
// Timestamp
"{{if .Timestamp}}\n {{$color.Level}}⇢{{$color.Reset}} {{$color.Timestamp}}{{.Timestamp}}{{$color.Reset}}{{end}}\n",
))
//\n {{$color}}⇢\033[0m {{if eq $v $caller}}\033[38;5;28m{{else}}\033[38;5;240m{{end}}#{{$k}} {{$v}}\033[0m{{end}}
textTemplate = template.Must(template.New("log").Parse(
// Timestamp
"{{if .Timestamp}} {{.LabelTime}}=\"{{.Timestamp}}\"{{end}} " +
// Level
"{{.LabelLevel}}=\"{{.Level}}\"" +
// Message
"{{if .Message}} {{.LabelMsg}}={{.Message}}{{end}}" +
// Error
"{{if .Err}} {{.LabelError}}=\"{{(printf \"%-v\" .Err)}}\"{{end}}" +
// Data fields
"{{$labelData := .LabelData}}{{range $k, $v := .Data}} {{if $labelData}}{{$labelData}}.{{end}}{{$k}}={{$v}}{{end}}" +
// Caller
"{{if .Caller}} {{.LabelCaller}}=\"{{.Caller}}\"{{end}}" +
// Hostname
"{{if .Hostname}} {{.LabelHost}}=\"{{.Hostname}}\"{{end}}" +
// Trace
"{{range $k, $v := .Trace}} trace.{{$k}}=\"{{$v}}\"{{end}}",
))
)
// TextFormatter formats logs into text.
type TextFormatter struct {
// DataKey allows users to put all the log entry parameters into a
// nested dictionary at a given key.
DataKey string
// DisableCaller disables caller data output.
DisableCaller bool
// DisableHostname disables hostname output.
DisableHostname bool
// DisableLevel disables level output.
DisableLevel bool
// DisableMessage disables message output.
DisableMessage bool
// DisableTimestamp disables timestamp output.
DisableTimestamp bool
// DisableTTY disables TTY formatted output.
DisableTTY bool
// Enable full backtrace output.
EnableTrace bool
// EscapeHTML is a flag that notes whether HTML characters should be
// escaped.
EscapeHTML bool
// ForceTTY forces TTY formatted output.
ForceTTY bool
// FieldMap allows users to customize the names of keys for default
// fields.
//
// For example:
// formatter := &TextFormatter{FieldMap: FieldMap{
// LabelCaller: "@caller",
// LabelData: "@data",
// LabelHost: "@hostname",
// LabelLevel: "@loglevel",
// LabelMsg: "@message",
// LabelTime: "@timestamp",
// }}
FieldMap FieldMap
// TimestampFormat allows a custom timestamp format to be used.
TimestampFormat string
// Flag noting whether the logger's out is to a terminal
isTerminal bool
sync.Once
}
func (f *TextFormatter) init(entry *Entry) {
if entry.Logger != nil {
f.isTerminal = checkIfTerminal(entry.Logger.Out)
}
}
// Format renders a single log entry
func (f *TextFormatter) Format(entry *Entry) ([]byte, error) {
var err error
prefixFieldClashes(entry.Data, f.FieldMap)
var logLine *bytes.Buffer
if entry.Buffer != nil {
logLine = entry.Buffer
} else {
logLine = &bytes.Buffer{}
}
f.Do(func() { f.init(entry) })
isTTY := (f.ForceTTY || f.isTerminal) && !f.DisableTTY
data := getData(entry, f.FieldMap, f.EscapeHTML, isTTY)
if f.DisableTimestamp {
data.Timestamp = ""
} else if "" != f.TimestampFormat {
data.Timestamp = entry.Time.Format(f.TimestampFormat)
} else {
data.Timestamp = entry.Time.Format(defaultTimestampFormat)
}
if f.DisableHostname {
data.Hostname = ""
}
if f.DisableCaller {
data.Caller = ""
}
if !f.EnableTrace {
data.Trace = []string{}
}
if e, ok := data.Err.(error); ok {
for nil != e {
data.ErrData = append(data.ErrData, escape(fmt.Sprintf("%-v", e), f.EscapeHTML))
e = errors.Unwrap(e.(error))
}
}
if isTTY {
for k, v := range data.Data {
switch tv := v.(type) {
case string:
data.Data[k] = `"` + tv + `"`
default:
data.Data[k] = v
}
}
err = termTemplate.Execute(logLine, data)
} else {
for k, v := range data.Data {
data.Data[k] = escape(v, f.EscapeHTML)
}
data.Message = escape(data.Message, f.EscapeHTML)
err = textTemplate.Execute(logLine, data)
}
if nil != err {
return nil, err
}
return append([]byte(strings.Trim(logLine.String(), " \n")), '\n'), nil
}