frostfs-node/pkg/util/logger/logger.go

52 lines
1.2 KiB
Go
Raw Normal View History

package logger
import (
2020-07-24 13:54:03 +00:00
"errors"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// Logger represents a component
2020-07-24 13:54:03 +00:00
// 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
}
2020-07-24 13:54:03 +00:00
// ErrNilLogger is returned by functions that
// expect a non-nil Logger but received nil.
2020-07-24 13:54:03 +00:00
var ErrNilLogger = errors.New("logger is nil")
// SetLevelString sets the 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)),
)
}