forked from gookit/slog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformatter_json.go
131 lines (113 loc) · 2.83 KB
/
formatter_json.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
package slog
import (
"encoding/json"
"github.com/valyala/bytebufferpool"
)
var (
// DefaultFields default log export fields for json formatter.
DefaultFields = []string{
FieldKeyDatetime,
FieldKeyChannel,
FieldKeyLevel,
FieldKeyCaller,
FieldKeyMessage,
FieldKeyData,
FieldKeyExtra,
}
// NoTimeFields log export fields without time
NoTimeFields = []string{
FieldKeyChannel,
FieldKeyLevel,
FieldKeyMessage,
FieldKeyData,
FieldKeyExtra,
}
)
// JSONFormatter definition
type JSONFormatter struct {
// Fields exported log fields. default is DefaultFields
Fields []string
// Aliases for output fields. you can change export field name.
//
// item: `"field" : "output name"`
// eg: {"message": "msg"} export field will display "msg"
Aliases StringMap
// PrettyPrint will indent all json logs
PrettyPrint bool
// TimeFormat the time format layout. default is DefaultTimeFormat
TimeFormat string
}
// NewJSONFormatter create new JSONFormatter
func NewJSONFormatter(fn ...func(*JSONFormatter)) *JSONFormatter {
f := &JSONFormatter{
// Aliases: make(StringMap, 0),
Fields: DefaultFields,
TimeFormat: DefaultTimeFormat,
}
if len(fn) > 0 {
fn[0](f)
}
return f
}
// Configure current formatter
func (f *JSONFormatter) Configure(fn func(*JSONFormatter)) *JSONFormatter {
fn(f)
return f
}
// AddField for export
func (f *JSONFormatter) AddField(name string) *JSONFormatter {
f.Fields = append(f.Fields, name)
return f
}
// Format an log record
func (f *JSONFormatter) Format(r *Record) ([]byte, error) {
logData := make(M, len(f.Fields))
for _, field := range f.Fields {
outName, ok := f.Aliases[field]
if !ok {
outName = field
}
switch {
case field == FieldKeyDatetime:
logData[outName] = r.Time.Format(f.TimeFormat)
case field == FieldKeyTimestamp:
logData[outName] = r.timestamp()
case field == FieldKeyCaller && r.Caller != nil:
logData[outName] = formatCaller(r.Caller, r.CallerFlag)
case field == FieldKeyLevel:
logData[outName] = r.LevelName()
case field == FieldKeyChannel:
logData[outName] = r.Channel
case field == FieldKeyMessage:
logData[outName] = r.Message
case field == FieldKeyData:
logData[outName] = r.Data
case field == FieldKeyExtra:
logData[outName] = r.Extra
// default:
// logData[outName] = r.Fields[field]
}
}
// exported custom fields
for field, value := range r.Fields {
fieldKey := field
if _, has := logData[field]; has {
fieldKey = "fields." + field
}
logData[fieldKey] = value
}
// sort.Interface()
buf := bytebufferpool.Get()
// buf.Reset()
defer bytebufferpool.Put(buf)
// buf := r.NewBuffer()
// buf.Reset()
// buf.Grow(256)
encoder := json.NewEncoder(buf)
if f.PrettyPrint {
encoder.SetIndent("", " ")
}
// has been added newline in Encode().
err := encoder.Encode(logData)
return buf.Bytes(), err
}