[#1619] logger: Filter entries by tags provided in config
Signed-off-by: Anton Nikiforov <an.nikiforov@yadro.com>
This commit is contained in:
parent
e90c5f6e23
commit
ec48044896
8 changed files with 163 additions and 8 deletions
|
@ -2,6 +2,8 @@ package logger
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"sync/atomic"
|
||||
|
||||
"git.frostfs.info/TrueCloudLab/zapjournald"
|
||||
"github.com/ssgreg/journald"
|
||||
|
@ -14,6 +16,9 @@ import (
|
|||
type Logger struct {
|
||||
z *zap.Logger
|
||||
lvl zap.AtomicLevel
|
||||
at *atomic.Uint32
|
||||
m uint32
|
||||
tl *atomic.Value
|
||||
}
|
||||
|
||||
// Prm groups Logger's parameters.
|
||||
|
@ -35,6 +40,9 @@ type Prm struct {
|
|||
|
||||
// PrependTimestamp specifies whether to prepend a timestamp in the log
|
||||
PrependTimestamp bool
|
||||
|
||||
// AllowedTags list of allowed tags with log level
|
||||
AllowedTags []string
|
||||
}
|
||||
|
||||
const (
|
||||
|
@ -110,7 +118,17 @@ func newConsoleLogger(prm Prm) (*Logger, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
l := &Logger{z: lZap, lvl: lvl}
|
||||
pat, tl, err := parseAllowedTags(prm.AllowedTags)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var at atomic.Uint32
|
||||
at.Store(pat)
|
||||
|
||||
v := atomic.Value{}
|
||||
v.Store(tl)
|
||||
|
||||
l := &Logger{z: lZap, lvl: lvl, m: 1 << TagMain, at: &at, tl: &v}
|
||||
|
||||
return l, nil
|
||||
}
|
||||
|
@ -142,13 +160,32 @@ func newJournaldLogger(prm Prm) (*Logger, error) {
|
|||
|
||||
lZap := zap.New(coreWithContext, zap.AddStacktrace(zap.NewAtomicLevelAt(zap.FatalLevel)), zap.AddCallerSkip(1))
|
||||
|
||||
l := &Logger{z: lZap, lvl: lvl}
|
||||
pat, tl, err := parseAllowedTags(prm.AllowedTags)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var at atomic.Uint32
|
||||
at.Store(pat)
|
||||
|
||||
v := atomic.Value{}
|
||||
v.Store(tl)
|
||||
|
||||
l := &Logger{z: lZap, lvl: lvl, m: 1 << TagMain, at: &at, tl: &v}
|
||||
|
||||
return l, nil
|
||||
}
|
||||
|
||||
func (l *Logger) Reload(prm Prm) {
|
||||
func (l *Logger) Reload(prm Prm) error {
|
||||
at, tl, err := parseAllowedTags(prm.AllowedTags)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
l.lvl.SetLevel(prm.level)
|
||||
l.at.Store(at)
|
||||
|
||||
l.tl.Store(tl)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *Logger) WithOptions(options ...zap.Option) {
|
||||
|
@ -156,11 +193,27 @@ func (l *Logger) WithOptions(options ...zap.Option) {
|
|||
}
|
||||
|
||||
func (l *Logger) With(fields ...zap.Field) *Logger {
|
||||
return &Logger{z: l.z.With(fields...)}
|
||||
c := *l
|
||||
c.z = l.z.With(fields...)
|
||||
return &c
|
||||
}
|
||||
|
||||
func (l *Logger) WithTag(bit uint8) *Logger {
|
||||
c := *l
|
||||
c.m = uint32(1 << bit)
|
||||
return &c
|
||||
}
|
||||
|
||||
func NewLoggerWrapper(z *zap.Logger) *Logger {
|
||||
at := &atomic.Uint32{}
|
||||
at.Store(math.MaxUint32)
|
||||
|
||||
tl := &atomic.Value{}
|
||||
tl.Store(make(map[uint32]zapcore.Level))
|
||||
|
||||
return &Logger{
|
||||
z: z.WithOptions(zap.AddCallerSkip(1)),
|
||||
z: z.WithOptions(zap.AddCallerSkip(1)),
|
||||
at: at,
|
||||
tl: tl,
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue