-
Notifications
You must be signed in to change notification settings - Fork 5
/
std_formatter.go
124 lines (106 loc) · 2.96 KB
/
std_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
package log
import (
"bytes"
"fmt"
"strings"
"text/template"
)
var (
stdTemplate = template.Must(template.New("log").Parse(
"{{if .Timestamp}}{{.Timestamp}}{{end}}" +
"{{if .Message}} {{.Message}}{{end}}" +
"{{if .Level}} {{.LabelLevel}}=\"{{.Level}}\"{{end}}" +
"{{$labelData := .LabelData}}" +
"{{range $k, $v := .Data}} " +
"{{if $labelData}}{{$labelData}}.{{end}}" +
"{{$k}}={{$v}}" +
"{{end}}" +
"{{if .Err}} {{.LabelError}}=\"{{.Err}}\"{{end}}" +
"{{if .Caller}} {{.LabelCaller}}=\"{{.Caller}}\"{{end}}" +
"{{if .Hostname}} {{.LabelHost}}=\"{{.Hostname}}\"{{end}}" +
"{{if .Err}} {{.LabelError}}=\"{{.Err}}\"{{end}}" +
"{{range $k, $v := .Trace}} trace.{{$k}}=\"{{$v}}\"{{end}}",
))
)
// StdFormatter formats logs into text.
type StdFormatter 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
// Enable full backtrace output.
EnableTrace bool
// EscapeHTML is a flag that notes whether HTML characters should be
// escaped.
EscapeHTML bool
// FieldMap allows users to customize the names of keys for default
// fields.
//
// For example:
// formatter := &StdFormatter{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
}
// Format renders a single log entry
func (f *StdFormatter) 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{}
}
data := getData(entry, f.FieldMap, f.EscapeHTML, false)
if f.DisableTimestamp {
data.Timestamp = ""
} else if "" != f.TimestampFormat {
data.Timestamp = entry.Time.Format(f.TimestampFormat)
} else {
data.Timestamp = entry.Time.Format("2006/01/02 15:04:05")
}
if f.DisableHostname {
data.Hostname = ""
}
if f.DisableCaller && !f.EnableTrace {
data.Caller = ""
}
if !f.EnableTrace {
data.Trace = []string{}
}
if nil != data.Err {
if _, ok := data.Err.(fmt.Formatter); !ok {
if e, ok := data.Err.(error); ok {
data.Err = e.Error()
}
}
}
for k, v := range data.Data {
if e, ok := v.(error); ok {
v = e.Error()
}
data.Data[k] = escape(v, f.EscapeHTML)
}
err = stdTemplate.Execute(logLine, data)
if nil != err {
return nil, err
}
return append([]byte(strings.Trim(logLine.String(), " \n")), '\n'), nil
}