mirror of
https://github.com/bakito/adguardhome-sync.git
synced 2025-03-01 01:24:43 +08:00
correct log fields
This commit is contained in:
parent
cf0a381f80
commit
1986e87518
1 changed files with 26 additions and 10 deletions
|
@ -29,32 +29,42 @@ func init() {
|
||||||
ErrorOutputPaths: []string{"stderr"},
|
ErrorOutputPaths: []string{"stderr"},
|
||||||
}
|
}
|
||||||
opt := zap.WrapCore(func(c zapcore.Core) zapcore.Core {
|
opt := zap.WrapCore(func(c zapcore.Core) zapcore.Core {
|
||||||
return zapcore.NewTee(c, &logList{enc: zapcore.NewConsoleEncoder(cfg.EncoderConfig)})
|
return zapcore.NewTee(c, &logList{
|
||||||
|
enc: zapcore.NewConsoleEncoder(cfg.EncoderConfig),
|
||||||
|
LevelEnabler: cfg.Level,
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
rootLogger, _ = cfg.Build(opt)
|
rootLogger, _ = cfg.Build(opt)
|
||||||
}
|
}
|
||||||
|
|
||||||
type logList struct {
|
type logList struct {
|
||||||
|
zapcore.LevelEnabler
|
||||||
enc zapcore.Encoder
|
enc zapcore.Encoder
|
||||||
fields []zapcore.Field
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *logList) Enabled(_ zapcore.Level) bool {
|
func (l *logList) clone() *logList {
|
||||||
return true
|
return &logList{
|
||||||
|
LevelEnabler: l.LevelEnabler,
|
||||||
|
enc: l.enc.Clone(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *logList) With(fields []zapcore.Field) zapcore.Core {
|
func (l *logList) With(fields []zapcore.Field) zapcore.Core {
|
||||||
l.fields = fields
|
clone := l.clone()
|
||||||
return l
|
addFields(clone.enc, fields)
|
||||||
|
return clone
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *logList) Check(ent zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.CheckedEntry {
|
func (l *logList) Check(ent zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.CheckedEntry {
|
||||||
|
if l.Enabled(ent.Level) {
|
||||||
return ce.AddCore(ent, l)
|
return ce.AddCore(ent, l)
|
||||||
|
}
|
||||||
|
return ce
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *logList) Write(ent zapcore.Entry, _ []zapcore.Field) error {
|
func (l *logList) Write(ent zapcore.Entry, fields []zapcore.Field) error {
|
||||||
buf, err := l.enc.EncodeEntry(ent, l.fields)
|
buf, err := l.enc.EncodeEntry(ent, fields)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -74,3 +84,9 @@ func (l *logList) Sync() error {
|
||||||
func Logs() []string {
|
func Logs() []string {
|
||||||
return logs
|
return logs
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func addFields(enc zapcore.ObjectEncoder, fields []zapcore.Field) {
|
||||||
|
for i := range fields {
|
||||||
|
fields[i].AddTo(enc)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue