-
Notifications
You must be signed in to change notification settings - Fork 1
/
core.go
163 lines (141 loc) · 3.79 KB
/
core.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
package ali
import (
"fmt"
"sync"
"go.uber.org/zap/buffer"
"go.uber.org/zap/zapcore"
)
var _ zapcore.Encoder = &mapObjEncoder{}
var encoderPool = sync.Pool{New: func() interface{} {
return &mapObjEncoder{MapObjectEncoder: zapcore.NewMapObjectEncoder()}
}}
func getEncoder() *mapObjEncoder {
return encoderPool.Get().(*mapObjEncoder)
}
func putEncoder(enc *mapObjEncoder) {
enc.MapObjectEncoder = zapcore.NewMapObjectEncoder()
encoderPool.Put(enc)
}
// mapObjEncoder ...
type mapObjEncoder struct {
*zapcore.EncoderConfig
parentFields []zapcore.Field
*zapcore.MapObjectEncoder
}
// newMapObjEncoder ...
func newMapObjEncoder(cfg zapcore.EncoderConfig) *mapObjEncoder {
return &mapObjEncoder{
EncoderConfig: &cfg,
MapObjectEncoder: zapcore.NewMapObjectEncoder(),
}
}
// Clone ...
func (e *mapObjEncoder) Clone() zapcore.Encoder {
return e.clone()
}
func (e *mapObjEncoder) clone() *mapObjEncoder {
clone := getEncoder()
clone.EncoderConfig = e.EncoderConfig
// copy parentFields
clone.parentFields = make([]zapcore.Field, 0, len(e.parentFields))
clone.parentFields = append(clone.parentFields, e.parentFields...)
// copy fields
for k, v := range e.MapObjectEncoder.Fields {
clone.MapObjectEncoder.Fields[k] = v
}
return clone
}
// EncodeEntry ...
func (e *mapObjEncoder) EncodeEntry(ent zapcore.Entry, fields []zapcore.Field) (*buffer.Buffer, error) {
// do nothing, just implement zapcore.Encoder.EncodeEntry()
return nil, nil
}
func (e *mapObjEncoder) encodeEntry(ent zapcore.Entry, fields []zapcore.Field) *mapObjEncoder {
final := e.clone()
if final.LevelKey != "" {
final.AddString(final.LevelKey, ent.Level.String())
}
if final.TimeKey != "" {
final.AddInt64(final.TimeKey, ent.Time.Unix())
}
if ent.LoggerName != "" && final.NameKey != "" {
final.AddString(final.NameKey, ent.LoggerName)
}
if ent.Caller.Defined && final.CallerKey != "" {
final.AddString(final.CallerKey, ent.Caller.String())
}
if final.MessageKey != "" {
final.AddString(final.MessageKey, ent.Message)
}
if ent.Stack != "" && final.StacktraceKey != "" {
final.AddString(final.StacktraceKey, ent.Stack)
}
addFields(final, fields)
return final
}
// NewCore creates a Core that writes logs to a WriteSyncer.
func NewCore(ops ...Option) (zapcore.Core, func() error) {
var c config
for _, o := range ops {
o(&c)
}
aliLs, err := newWriter(c)
if err != nil {
panic(fmt.Errorf("NewCore fail, %w", err))
}
core := &ioCore{
LevelEnabler: c.levelEnabler,
enc: c.encoder,
writer: aliLs,
}
closeFunc := func() error {
core.writer.cancel()
return core.writer.flush()
}
return core, closeFunc
}
func addFields(enc zapcore.ObjectEncoder, fields []zapcore.Field) {
for i := range fields {
fields[i].AddTo(enc)
}
}
type ioCore struct {
zapcore.LevelEnabler
enc zapcore.Encoder
writer *writer
}
func (c *ioCore) With(fields []zapcore.Field) zapcore.Core {
c.enc.(*mapObjEncoder).parentFields = fields
clone := c.clone()
// NOTICE: we must reset parentFields otherwise parent logger with also print parent fields
c.enc.(*mapObjEncoder).parentFields = nil
return clone
}
func (c *ioCore) Check(ent zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.CheckedEntry {
if c.Enabled(ent.Level) {
return ce.AddCore(ent, c)
}
return ce
}
func (c *ioCore) Write(ent zapcore.Entry, fields []zapcore.Field) (err error) {
clone := c.enc.(*mapObjEncoder).encodeEntry(ent, fields)
addFields(clone, append(fields, clone.parentFields...))
if err := c.writer.write(clone.Fields); err != nil {
return err
}
if ent.Level > zapcore.ErrorLevel {
err = c.Sync()
}
putEncoder(clone)
return
}
func (c *ioCore) Sync() error {
return c.writer.flush()
}
func (c *ioCore) clone() *ioCore {
return &ioCore{
LevelEnabler: c.LevelEnabler,
enc: c.enc.Clone(),
writer: c.writer,
}
}