frostfs-node/pkg/util/logger/logger.go
Leonard Lyubich db2e43387b [#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>
2021-05-11 12:35:12 +03:00

51 lines
1.2 KiB
Go

package logger
import (
"errors"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// Logger represents the component
// for writing messages to log.
//
// It is a type alias of
// go.uber.org/zap.Logger.
type Logger = zap.Logger
// Prm groups Logger's parameters.
type Prm struct {
level zapcore.Level
}
// ErrNilLogger is returned by functions that
// expect a non-nil Logger, but received nil.
var ErrNilLogger = errors.New("logger is nil")
// SetLevelString sets minimum logging level.
//
// 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.Level = zap.NewAtomicLevelAt(prm.level)
c.Encoding = "console"
c.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
return c.Build(
zap.AddStacktrace(zap.NewAtomicLevelAt(zap.FatalLevel)),
)
}