[#1619] logger: Filter entries by tags provided in config
Signed-off-by: Anton Nikiforov <an.nikiforov@yadro.com>
This commit is contained in:
parent
5e0223c20d
commit
d62b409961
8 changed files with 160 additions and 9 deletions
|
@ -2,6 +2,8 @@ package logger
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"sync/atomic"
|
||||
|
||||
"git.frostfs.info/TrueCloudLab/zapjournald"
|
||||
"github.com/ssgreg/journald"
|
||||
|
@ -14,6 +16,12 @@ import (
|
|||
type Logger struct {
|
||||
z *zap.Logger
|
||||
lvl zap.AtomicLevel
|
||||
// Contains uint32, each bit describes allowed tags for logging(0 - disabled, 1 - enabled)
|
||||
at *atomic.Uint32
|
||||
// Bit mask describes the tag allowed for Logger
|
||||
m uint32
|
||||
// Contains map of tag's bit masks to log level, overrides lvl
|
||||
tl *atomic.Value
|
||||
}
|
||||
|
||||
// Prm groups Logger's parameters.
|
||||
|
@ -35,6 +43,12 @@ type Prm struct {
|
|||
|
||||
// PrependTimestamp specifies whether to prepend a timestamp in the log
|
||||
PrependTimestamp bool
|
||||
|
||||
// each bit describes allowed tags for logging(0 - disabled, 1 - enabled)
|
||||
at uint32
|
||||
|
||||
// map of tag's bit masks to log level, overrides lvl
|
||||
tl map[uint32]zapcore.Level
|
||||
}
|
||||
|
||||
const (
|
||||
|
@ -64,6 +78,12 @@ func (p *Prm) SetDestination(d string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// SetAllowedTags parses list of allowed tags with log level.
|
||||
func (p *Prm) SetAllowedTags(tags []string) (err error) {
|
||||
p.at, p.tl, err = parseAllowedTags(tags)
|
||||
return err
|
||||
}
|
||||
|
||||
// NewLogger constructs a new zap logger instance. Constructing with nil
|
||||
// parameters is safe: default values will be used then.
|
||||
// Passing non-nil parameters after a successful creation (non-error) allows
|
||||
|
@ -87,10 +107,8 @@ func NewLogger(prm Prm) (*Logger, error) {
|
|||
}
|
||||
|
||||
func newConsoleLogger(prm Prm) (*Logger, error) {
|
||||
lvl := zap.NewAtomicLevelAt(prm.level)
|
||||
|
||||
c := zap.NewProductionConfig()
|
||||
c.Level = lvl
|
||||
c.Level = zap.NewAtomicLevelAt(zapcore.DebugLevel)
|
||||
c.Encoding = "console"
|
||||
if prm.SamplingHook != nil {
|
||||
c.Sampling.Hook = prm.SamplingHook
|
||||
|
@ -110,14 +128,19 @@ func newConsoleLogger(prm Prm) (*Logger, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
l := &Logger{z: lZap, lvl: lvl}
|
||||
var at atomic.Uint32
|
||||
at.Store(prm.at)
|
||||
|
||||
v := atomic.Value{}
|
||||
v.Store(prm.tl)
|
||||
|
||||
l := &Logger{z: lZap, lvl: zap.NewAtomicLevelAt(prm.level), m: 1 << TagMain, at: &at, tl: &v}
|
||||
|
||||
return l, nil
|
||||
}
|
||||
|
||||
func newJournaldLogger(prm Prm) (*Logger, error) {
|
||||
lvl := zap.NewAtomicLevelAt(prm.level)
|
||||
|
||||
lvl := zap.NewAtomicLevelAt(zapcore.DebugLevel)
|
||||
c := zap.NewProductionConfig()
|
||||
c.Level = lvl
|
||||
c.Encoding = "console"
|
||||
|
@ -142,13 +165,21 @@ 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}
|
||||
var at atomic.Uint32
|
||||
at.Store(prm.at)
|
||||
|
||||
v := atomic.Value{}
|
||||
v.Store(prm.tl)
|
||||
|
||||
l := &Logger{z: lZap, lvl: zap.NewAtomicLevelAt(prm.level), m: 1 << TagMain, at: &at, tl: &v}
|
||||
|
||||
return l, nil
|
||||
}
|
||||
|
||||
func (l *Logger) Reload(prm Prm) {
|
||||
l.lvl.SetLevel(prm.level)
|
||||
l.at.Store(prm.at)
|
||||
l.tl.Store(prm.tl)
|
||||
}
|
||||
|
||||
func (l *Logger) WithOptions(options ...zap.Option) {
|
||||
|
@ -156,11 +187,28 @@ 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,
|
||||
lvl: zap.NewAtomicLevelAt(zapcore.DebugLevel),
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue