forked from TrueCloudLab/frostfs-node
[#1619] logger: Allow to set options for zap.Logger
via logger.Prm
Change-Id: I8eed951c25d1ecf18b0aea62c6825be65a450085 Signed-off-by: Anton Nikiforov <an.nikiforov@yadro.com>
This commit is contained in:
parent
9358938222
commit
3bbee1b554
2 changed files with 23 additions and 13 deletions
|
@ -108,6 +108,7 @@ type applicationConfiguration struct {
|
||||||
level string
|
level string
|
||||||
destination string
|
destination string
|
||||||
timestamp bool
|
timestamp bool
|
||||||
|
options []zap.Option
|
||||||
}
|
}
|
||||||
|
|
||||||
ObjectCfg struct {
|
ObjectCfg struct {
|
||||||
|
@ -232,6 +233,14 @@ func (a *applicationConfiguration) readConfig(c *config.Config) error {
|
||||||
a.LoggerCfg.level = loggerconfig.Level(c)
|
a.LoggerCfg.level = loggerconfig.Level(c)
|
||||||
a.LoggerCfg.destination = loggerconfig.Destination(c)
|
a.LoggerCfg.destination = loggerconfig.Destination(c)
|
||||||
a.LoggerCfg.timestamp = loggerconfig.Timestamp(c)
|
a.LoggerCfg.timestamp = loggerconfig.Timestamp(c)
|
||||||
|
var opts []zap.Option
|
||||||
|
if loggerconfig.ToLokiConfig(c).Enabled {
|
||||||
|
opts = []zap.Option{zap.WrapCore(func(core zapcore.Core) zapcore.Core {
|
||||||
|
lokiCore := lokicore.New(core, loggerconfig.ToLokiConfig(c))
|
||||||
|
return lokiCore
|
||||||
|
})}
|
||||||
|
}
|
||||||
|
a.LoggerCfg.options = opts
|
||||||
|
|
||||||
// Object
|
// Object
|
||||||
|
|
||||||
|
@ -718,12 +727,6 @@ func initCfg(appCfg *config.Config) *cfg {
|
||||||
logPrm.SamplingHook = c.metricsCollector.LogMetrics().GetSamplingHook()
|
logPrm.SamplingHook = c.metricsCollector.LogMetrics().GetSamplingHook()
|
||||||
log, err := logger.NewLogger(logPrm)
|
log, err := logger.NewLogger(logPrm)
|
||||||
fatalOnErr(err)
|
fatalOnErr(err)
|
||||||
if loggerconfig.ToLokiConfig(appCfg).Enabled {
|
|
||||||
log.WithOptions(zap.WrapCore(func(core zapcore.Core) zapcore.Core {
|
|
||||||
lokiCore := lokicore.New(core, loggerconfig.ToLokiConfig(appCfg))
|
|
||||||
return lokiCore
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
|
|
||||||
c.internals = initInternals(appCfg, log)
|
c.internals = initInternals(appCfg, log)
|
||||||
|
|
||||||
|
@ -1090,6 +1093,7 @@ func (c *cfg) loggerPrm() (logger.Prm, error) {
|
||||||
return logger.Prm{}, errors.New("incorrect log destination format: " + c.LoggerCfg.destination)
|
return logger.Prm{}, errors.New("incorrect log destination format: " + c.LoggerCfg.destination)
|
||||||
}
|
}
|
||||||
prm.PrependTimestamp = c.LoggerCfg.timestamp
|
prm.PrependTimestamp = c.LoggerCfg.timestamp
|
||||||
|
prm.Options = c.LoggerCfg.options
|
||||||
|
|
||||||
return prm, nil
|
return prm, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,6 +36,9 @@ type Prm struct {
|
||||||
|
|
||||||
// PrependTimestamp specifies whether to prepend a timestamp in the log
|
// PrependTimestamp specifies whether to prepend a timestamp in the log
|
||||||
PrependTimestamp bool
|
PrependTimestamp bool
|
||||||
|
|
||||||
|
// Options for zap.Logger
|
||||||
|
Options []zap.Option
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -103,10 +106,12 @@ func newConsoleLogger(prm Prm) (*Logger, error) {
|
||||||
c.EncoderConfig.TimeKey = ""
|
c.EncoderConfig.TimeKey = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
lZap, err := c.Build(
|
opts := []zap.Option{
|
||||||
zap.AddStacktrace(zap.NewAtomicLevelAt(zap.FatalLevel)),
|
zap.AddStacktrace(zap.NewAtomicLevelAt(zap.FatalLevel)),
|
||||||
zap.AddCallerSkip(1),
|
zap.AddCallerSkip(1),
|
||||||
)
|
}
|
||||||
|
opts = append(opts, prm.Options...)
|
||||||
|
lZap, err := c.Build(opts...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -150,7 +155,12 @@ func newJournaldLogger(prm Prm) (*Logger, error) {
|
||||||
c.Sampling.Thereafter,
|
c.Sampling.Thereafter,
|
||||||
samplerOpts...,
|
samplerOpts...,
|
||||||
)
|
)
|
||||||
lZap := zap.New(samplingCore, zap.AddStacktrace(zap.NewAtomicLevelAt(zap.FatalLevel)), zap.AddCallerSkip(1))
|
opts := []zap.Option{
|
||||||
|
zap.AddStacktrace(zap.NewAtomicLevelAt(zap.FatalLevel)),
|
||||||
|
zap.AddCallerSkip(1),
|
||||||
|
}
|
||||||
|
opts = append(opts, prm.Options...)
|
||||||
|
lZap := zap.New(samplingCore, opts...)
|
||||||
|
|
||||||
l := &Logger{z: lZap, lvl: lvl}
|
l := &Logger{z: lZap, lvl: lvl}
|
||||||
|
|
||||||
|
@ -161,10 +171,6 @@ func (l *Logger) Reload(prm Prm) {
|
||||||
l.lvl.SetLevel(prm.level)
|
l.lvl.SetLevel(prm.level)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Logger) WithOptions(options ...zap.Option) {
|
|
||||||
l.z = l.z.WithOptions(options...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *Logger) With(fields ...zap.Field) *Logger {
|
func (l *Logger) With(fields ...zap.Field) *Logger {
|
||||||
return &Logger{z: l.z.With(fields...)}
|
return &Logger{z: l.z.With(fields...)}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue