/
pretty_zap_console.go
251 lines (218 loc) · 7.79 KB
/
pretty_zap_console.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package zaputils
import (
"encoding/json"
"fmt"
"github.com/cyberax/go-dd-service-base/utils"
"github.com/cyberax/go-dd-service-base/visibility"
"go.uber.org/zap/buffer"
"go.uber.org/zap/zapcore"
"strings"
"time"
)
type prettyConsoleEncoder struct {
zapcore.Encoder
cfg zapcore.EncoderConfig
}
var pool = buffer.NewPool()
// NewConsoleEncoder creates an encoder whose output is designed for human -
// rather than machine - consumption. It serializes the core log entry data
// (message, level, timestamp, etc.) in a plain-text format and leaves the
// structured context as pretty-printed multiline JSON.
//
// Additional functionality includes easily-readable stack traces.
//
// Note that while pretty-printing is useful in development, it's bad for production
func NewPrettyConsoleEncoder(cfg zapcore.EncoderConfig) zapcore.Encoder {
// Use empty config because we don't care about encoding informational
// fields, we only want to use it to encode extra fields.
encoder := zapcore.NewJSONEncoder(zapcore.EncoderConfig{
EncodeLevel: cfg.EncodeLevel,
EncodeTime: cfg.EncodeTime,
EncodeDuration: cfg.EncodeDuration,
EncodeCaller: cfg.EncodeCaller,
EncodeName: cfg.EncodeName,
})
return &prettyConsoleEncoder{cfg: cfg, Encoder: encoder}
}
func (c *prettyConsoleEncoder) Clone() zapcore.Encoder {
return &prettyConsoleEncoder{cfg: c.cfg, Encoder: c.Encoder.Clone()}
}
func (c *prettyConsoleEncoder) EncodeEntry(ent zapcore.Entry,
fields []zapcore.Field) (*buffer.Buffer, error) {
line := pool.Get()
// We don't want the entry's metadata to be quoted and escaped (if it's
// encoded as strings), which means that we can't use the JSON encoder. The
// simplest option is to use the memory encoder and fmt.Fprint.
//
// If this ever becomes a performance bottleneck, we can implement
// ArrayEncoder for our plain-text format.
arr := &sliceArrayEncoder{}
if c.cfg.TimeKey != "" && c.cfg.EncodeTime != nil {
c.cfg.EncodeTime(ent.Time, arr)
}
if c.cfg.LevelKey != "" && c.cfg.EncodeLevel != nil {
c.cfg.EncodeLevel(ent.Level, arr)
}
if ent.LoggerName != "" && c.cfg.NameKey != "" {
nameEncoder := c.cfg.EncodeName
if nameEncoder == nil {
// Fall back to FullNameEncoder for backward compatibility.
nameEncoder = zapcore.FullNameEncoder
}
nameEncoder(ent.LoggerName, arr)
}
if ent.Caller.Defined && c.cfg.CallerKey != "" && c.cfg.EncodeCaller != nil {
c.cfg.EncodeCaller(ent.Caller, arr)
}
for i := range arr.elems {
if i > 0 {
line.AppendByte('\t')
}
_, _ = fmt.Fprint(line, arr.elems[i])
}
// Add the message itself.
if c.cfg.MessageKey != "" {
c.addTabIfNecessary(line)
line.AppendString(ent.Message)
}
// Add any structured context.
c.writeContext(line, fields)
// If there's no stacktrace key, honor that; this allows users to force
// single-line output.
if ent.Stack != "" && c.cfg.StacktraceKey != "" {
line.AppendByte('\n')
line.AppendString(ent.Stack)
}
if c.cfg.LineEnding != "" {
line.AppendString(c.cfg.LineEnding)
} else {
line.AppendString(zapcore.DefaultLineEnding)
}
return line, nil
}
func (c *prettyConsoleEncoder) writeContext(line *buffer.Buffer, extra []zapcore.Field) {
context := c.Encoder.Clone()
ent := zapcore.Entry{
Level: 0,
Time: time.Time{},
LoggerName: "",
Message: "",
Caller: zapcore.EntryCaller{},
Stack: "",
}
buf, _ := context.EncodeEntry(ent, extra)
if buf == nil {
return
}
defer buf.Free()
c.addTabIfNecessary(line)
// Make sure we always have something to write
defer line.TrimNewline()
fieldsToPrint := []byte(strings.TrimRight(buf.String(), "\r\n"))
cleanuper := utils.NewCleanup(func() {
_, _ = line.Write(fieldsToPrint)
})
defer cleanuper.Cleanup()
var fieldsData map[string]interface{}
err := json.Unmarshal(fieldsToPrint, &fieldsData)
if err != nil {
return
}
stack, hasStack := c.tryGetStack(fieldsData)
if !hasStack {
return
}
// Remove the stack trace data
delete(fieldsData, "stacktrace")
// Format the rest of the fields
withoutStack, err := json.Marshal(fieldsData)
if err != nil {
return
}
cleanuper.Disarm()
_, _ = line.Write(withoutStack)
if hasStack {
_, _ = line.Write([]byte(stack))
}
}
func (c *prettyConsoleEncoder) tryGetStack(fieldsData map[string]interface{}) (string, bool) {
panicText, hasPanic := fieldsData["panic"]
// Print the stack trace in a nice and separated way
stack, ok := fieldsData["stacktrace"]
if !ok {
return "", false
}
if stackStr, ok := stack.(string); ok {
// Add a "\t" prefix to each line
stackStr = strings.TrimSpace(stackStr)
stackStr = strings.Join(strings.Split(stackStr, "\n"), "\n\t")
if hasPanic {
return fmt.Sprintf("\nPanic: %v\n\t%s", panicText, stackStr), true
}
return "\n\t" + stackStr, true
}
// Try to transmute the stack back into StackElements
data, err := json.Marshal(stack)
if err != nil {
return "", false
}
var transmutedStack []visibility.StackElement
err = json.Unmarshal(data, &transmutedStack)
if err != nil {
return "", false
}
stackStr := "\n"
if hasPanic {
stackStr = fmt.Sprintf("\nPanic: %v\n", panicText)
}
for _, s := range transmutedStack {
stackStr += fmt.Sprintf("\t%s %s\n", s.Fl, s.Fn)
}
return stackStr, true
}
func (c *prettyConsoleEncoder) addTabIfNecessary(line *buffer.Buffer) {
if line.Len() > 0 {
line.AppendByte('\t')
}
}
// sliceArrayEncoder is an ArrayEncoder backed by a simple []interface{}. Like
// the MapObjectEncoder, it's not designed for production use.
type sliceArrayEncoder struct {
elems []interface{}
}
func (s *sliceArrayEncoder) AppendArray(v zapcore.ArrayMarshaler) error {
enc := &sliceArrayEncoder{}
err := v.MarshalLogArray(enc)
s.elems = append(s.elems, enc.elems)
return err
}
func (s *sliceArrayEncoder) AppendObject(v zapcore.ObjectMarshaler) error {
m := zapcore.NewMapObjectEncoder()
err := v.MarshalLogObject(m)
s.elems = append(s.elems, m.Fields)
return err
}
func (s *sliceArrayEncoder) AppendReflected(v interface{}) error {
s.elems = append(s.elems, v)
return nil
}
func (s *sliceArrayEncoder) AppendBool(v bool) { s.elems = append(s.elems, v) }
func (s *sliceArrayEncoder) AppendByteString(v []byte) { s.elems = append(s.elems, string(v)) }
func (s *sliceArrayEncoder) AppendComplex128(v complex128) { s.elems = append(s.elems, v) }
func (s *sliceArrayEncoder) AppendComplex64(v complex64) { s.elems = append(s.elems, v) }
func (s *sliceArrayEncoder) AppendDuration(v time.Duration) { s.elems = append(s.elems, v) }
func (s *sliceArrayEncoder) AppendFloat64(v float64) { s.elems = append(s.elems, v) }
func (s *sliceArrayEncoder) AppendFloat32(v float32) { s.elems = append(s.elems, v) }
func (s *sliceArrayEncoder) AppendInt(v int) { s.elems = append(s.elems, v) }
func (s *sliceArrayEncoder) AppendInt64(v int64) { s.elems = append(s.elems, v) }
func (s *sliceArrayEncoder) AppendInt32(v int32) { s.elems = append(s.elems, v) }
func (s *sliceArrayEncoder) AppendInt16(v int16) { s.elems = append(s.elems, v) }
func (s *sliceArrayEncoder) AppendInt8(v int8) { s.elems = append(s.elems, v) }
func (s *sliceArrayEncoder) AppendString(v string) { s.elems = append(s.elems, v) }
func (s *sliceArrayEncoder) AppendTime(v time.Time) { s.elems = append(s.elems, v) }
func (s *sliceArrayEncoder) AppendUint(v uint) { s.elems = append(s.elems, v) }
func (s *sliceArrayEncoder) AppendUint64(v uint64) { s.elems = append(s.elems, v) }
func (s *sliceArrayEncoder) AppendUint32(v uint32) { s.elems = append(s.elems, v) }
func (s *sliceArrayEncoder) AppendUint16(v uint16) { s.elems = append(s.elems, v) }
func (s *sliceArrayEncoder) AppendUint8(v uint8) { s.elems = append(s.elems, v) }
func (s *sliceArrayEncoder) AppendUintptr(v uintptr) { s.elems = append(s.elems, v) }