-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencoder.go
80 lines (69 loc) · 2.1 KB
/
encoder.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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package fabenc
import (
"io"
"time"
zaplogfmt "github.com/sykesm/zap-logfmt"
"go.uber.org/zap/buffer"
"go.uber.org/zap/zapcore"
)
// A FormatEncoder is a zapcore.Encoder that formats log records according to a
// go-logging based format specifier.
type FormatEncoder struct {
zapcore.Encoder
formatters []Formatter
pool buffer.Pool
}
// A Formatter is used to format and write data from a zap log entry.
type Formatter interface {
Format(w io.Writer, entry zapcore.Entry, fields []zapcore.Field)
}
func NewFormatEncoder(formatters ...Formatter) *FormatEncoder {
return &FormatEncoder{
Encoder: zaplogfmt.NewEncoder(zapcore.EncoderConfig{
MessageKey: "", // disable
LevelKey: "", // disable
TimeKey: "", // disable
NameKey: "", // disable
CallerKey: "", // disable
StacktraceKey: "", // disable
LineEnding: "\n",
EncodeDuration: zapcore.StringDurationEncoder,
EncodeTime: func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(t.Format("2006-01-02T15:04:05.999Z07:00"))
},
}),
formatters: formatters,
pool: buffer.NewPool(),
}
}
// Clone creates a new instance of this encoder with the same configuration.
func (f *FormatEncoder) Clone() zapcore.Encoder {
return &FormatEncoder{
Encoder: f.Encoder.Clone(),
formatters: f.formatters,
pool: f.pool,
}
}
// EncodeEntry formats a zap log record. The structured fields are formatted by a
// zapcore.ConsoleEncoder and are appended as JSON to the end of the formatted entry.
// All entries are terminated by a newline.
func (f *FormatEncoder) EncodeEntry(entry zapcore.Entry, fields []zapcore.Field) (*buffer.Buffer, error) {
line := f.pool.Get()
for _, f := range f.formatters {
f.Format(line, entry, fields)
}
encodedFields, err := f.Encoder.EncodeEntry(entry, fields)
if err != nil {
return nil, err
}
if line.Len() > 0 && encodedFields.Len() != 1 {
line.AppendString(" ")
}
line.AppendString(encodedFields.String())
encodedFields.Free()
return line, nil
}