-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloggerx-core.go
56 lines (45 loc) · 1.11 KB
/
loggerx-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
package zapsetup
import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
type CoreX struct {
core zapcore.Core
sink Sink
logLevelForSink zapcore.Level
}
func NewCoreX(l *zap.Logger, sink Sink) *CoreX {
return &CoreX{
core: l.Core(),
sink: sink,
logLevelForSink: zapcore.DebugLevel,
}
}
func (c *CoreX) Enabled(level zapcore.Level) bool {
return c.core.Enabled(level)
}
func (c *CoreX) Check(ent zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.CheckedEntry {
if c.Enabled(ent.Level) {
return ce.AddCore(ent, c)
}
return ce
}
func (c *CoreX) With(fields []zapcore.Field) zapcore.Core {
return c.core.With(fields)
}
func (c *CoreX) Write(ent zapcore.Entry, fields []zapcore.Field) error {
if c.logLevelForSink <= ent.Level {
c.sink.Write(ent, fields)
return nil
}
return c.core.Write(ent, fields)
}
func (c *CoreX) Sync() error {
return c.core.Sync()
}
// SetLogLevelForSink doesn't change the log level of the core,
// but it changes the log level of the sink
// Note: it's not goroutine safe
func (c *CoreX) SetLogLevelForSink(level zapcore.Level) {
c.logLevelForSink = level
}