forked from zlyuancn/zlog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextra_log.go
49 lines (45 loc) · 1.07 KB
/
extra_log.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
/*
-------------------------------------------------
Author : Zhang Fan
date: 2020/4/17
Description :
-------------------------------------------------
*/
package zlog
import (
"go.uber.org/multierr"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
type extraLogger struct {
zapcore.Core
logs []*logWrap
}
// 添加额外的输出
func WithExtraLogger(logs ...*logWrap) zap.Option {
return zap.WrapCore(func(core zapcore.Core) zapcore.Core {
return &extraLogger{
Core: core,
logs: append(([]*logWrap)(nil), logs...),
}
})
}
func (c *extraLogger) Check(ent zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.CheckedEntry {
if c.Enabled(ent.Level) {
return ce.AddCore(ent, c)
}
return ce
}
func (h *extraLogger) With(fields []zapcore.Field) zapcore.Core {
return &extraLogger{
Core: h.Core.With(fields),
logs: h.logs,
}
}
func (c *extraLogger) Write(ent zapcore.Entry, fields []zapcore.Field) error {
err := c.Core.Write(ent, fields)
for i := range c.logs {
err = multierr.Append(err, c.logs[i].Core().Write(ent, fields))
}
return err
}