forked from TrueCloudLab/frostfs-node
[#493] logger: Simplify Logger's parameters
Replace `viper.Viper` parameter with `Prm` structure. Currently only logging level can be parameterized through string setter. Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
parent
2a970b0125
commit
db2e43387b
4 changed files with 39 additions and 25 deletions
|
@ -44,7 +44,14 @@ func main() {
|
||||||
cfg, err := newConfig(*configFile)
|
cfg, err := newConfig(*configFile)
|
||||||
exitErr(err)
|
exitErr(err)
|
||||||
|
|
||||||
log, err := logger.NewLogger(cfg)
|
var logPrm logger.Prm
|
||||||
|
|
||||||
|
err = logPrm.SetLevelString(
|
||||||
|
cfg.GetString("logger.level"),
|
||||||
|
)
|
||||||
|
exitErr(err)
|
||||||
|
|
||||||
|
log, err := logger.NewLogger(logPrm)
|
||||||
exitErr(err)
|
exitErr(err)
|
||||||
|
|
||||||
log = log.With(
|
log = log.With(
|
||||||
|
|
|
@ -352,7 +352,14 @@ func initCfg(path string) *cfg {
|
||||||
viperCfg.GetString(cfgReputationContract))
|
viperCfg.GetString(cfgReputationContract))
|
||||||
fatalOnErr(err)
|
fatalOnErr(err)
|
||||||
|
|
||||||
log, err := logger.NewLogger(viperCfg)
|
var logPrm logger.Prm
|
||||||
|
|
||||||
|
err = logPrm.SetLevelString(
|
||||||
|
viperCfg.GetString(cfgLogLevel),
|
||||||
|
)
|
||||||
|
fatalOnErr(err)
|
||||||
|
|
||||||
|
log, err := logger.NewLogger(logPrm)
|
||||||
fatalOnErr(err)
|
fatalOnErr(err)
|
||||||
|
|
||||||
log = log.With(
|
log = log.With(
|
||||||
|
|
BIN
go.sum
BIN
go.sum
Binary file not shown.
|
@ -2,9 +2,7 @@ package logger
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/spf13/viper"
|
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
"go.uber.org/zap/zapcore"
|
"go.uber.org/zap/zapcore"
|
||||||
)
|
)
|
||||||
|
@ -16,36 +14,38 @@ import (
|
||||||
// go.uber.org/zap.Logger.
|
// go.uber.org/zap.Logger.
|
||||||
type Logger = zap.Logger
|
type Logger = zap.Logger
|
||||||
|
|
||||||
|
// Prm groups Logger's parameters.
|
||||||
|
type Prm struct {
|
||||||
|
level zapcore.Level
|
||||||
|
}
|
||||||
|
|
||||||
// ErrNilLogger is returned by functions that
|
// ErrNilLogger is returned by functions that
|
||||||
// expect a non-nil Logger, but received nil.
|
// expect a non-nil Logger, but received nil.
|
||||||
var ErrNilLogger = errors.New("logger is nil")
|
var ErrNilLogger = errors.New("logger is nil")
|
||||||
|
|
||||||
// NewLogger is a logger's constructor.
|
// SetLevelString sets minimum logging level.
|
||||||
func NewLogger(v *viper.Viper) (*Logger, error) {
|
//
|
||||||
|
// Returns error of s is not a string representation of zap.Level
|
||||||
|
// value (see zapcore.Level docs).
|
||||||
|
func (p *Prm) SetLevelString(s string) error {
|
||||||
|
return p.level.UnmarshalText([]byte(s))
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewLogger constructs a new zap logger instance.
|
||||||
|
//
|
||||||
|
// Logger is built from production logging configuration with:
|
||||||
|
// * parameterized level;
|
||||||
|
// * console encoding;
|
||||||
|
// * ISO8601 time encoding.
|
||||||
|
//
|
||||||
|
// Logger records a stack trace for all messages at or above fatal level.
|
||||||
|
func NewLogger(prm Prm) (*Logger, error) {
|
||||||
c := zap.NewProductionConfig()
|
c := zap.NewProductionConfig()
|
||||||
c.Level = safeLevel(v.GetString("logger.level"))
|
c.Level = zap.NewAtomicLevelAt(prm.level)
|
||||||
c.Encoding = "console"
|
c.Encoding = "console"
|
||||||
c.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
|
c.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
|
||||||
|
|
||||||
return c.Build(
|
return c.Build(
|
||||||
// record a stack trace for all messages at or above fatal level
|
|
||||||
zap.AddStacktrace(zap.NewAtomicLevelAt(zap.FatalLevel)),
|
zap.AddStacktrace(zap.NewAtomicLevelAt(zap.FatalLevel)),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func safeLevel(lvl string) zap.AtomicLevel {
|
|
||||||
switch strings.ToLower(lvl) {
|
|
||||||
case "debug":
|
|
||||||
return zap.NewAtomicLevelAt(zap.DebugLevel)
|
|
||||||
case "warn":
|
|
||||||
return zap.NewAtomicLevelAt(zap.WarnLevel)
|
|
||||||
case "error":
|
|
||||||
return zap.NewAtomicLevelAt(zap.ErrorLevel)
|
|
||||||
case "fatal":
|
|
||||||
return zap.NewAtomicLevelAt(zap.FatalLevel)
|
|
||||||
case "panic":
|
|
||||||
return zap.NewAtomicLevelAt(zap.PanicLevel)
|
|
||||||
default:
|
|
||||||
return zap.NewAtomicLevelAt(zap.InfoLevel)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue