2020-07-10 14:17:51 +00:00
|
|
|
package logger
|
|
|
|
|
|
|
|
import (
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"go.uber.org/zap/zapcore"
|
|
|
|
)
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// Logger represents a component
|
2020-07-24 13:54:03 +00:00
|
|
|
// for writing messages to log.
|
2022-09-28 07:41:01 +00:00
|
|
|
type Logger struct {
|
|
|
|
*zap.Logger
|
|
|
|
lvl zap.AtomicLevel
|
|
|
|
}
|
2020-07-24 13:54:03 +00:00
|
|
|
|
2021-05-11 08:54:59 +00:00
|
|
|
// Prm groups Logger's parameters.
|
2022-09-28 07:59:23 +00:00
|
|
|
// Successful passing non-nil parameters to the NewLogger (if returned
|
|
|
|
// error is nil) connects the parameters with the returned Logger.
|
|
|
|
// Parameters that have been connected to the Logger support its
|
|
|
|
// configuration changing.
|
|
|
|
//
|
|
|
|
// Passing Prm after a successful connection via the NewLogger, connects
|
|
|
|
// the Prm to a new instance of the Logger.
|
|
|
|
//
|
|
|
|
// See also Reload, SetLevelString.
|
2021-05-11 08:54:59 +00:00
|
|
|
type Prm struct {
|
2022-09-28 07:59:23 +00:00
|
|
|
// link to the created Logger
|
|
|
|
// instance; used for a runtime
|
|
|
|
// reconfiguration
|
|
|
|
_log *Logger
|
|
|
|
|
|
|
|
// support runtime rereading
|
2021-05-11 08:54:59 +00:00
|
|
|
level zapcore.Level
|
|
|
|
|
2023-10-08 09:32:00 +00:00
|
|
|
// SamplingHook hook for the zap.Logger
|
|
|
|
SamplingHook func(e zapcore.Entry, sd zapcore.SamplingDecision)
|
2023-06-09 08:47:39 +00:00
|
|
|
|
2022-09-28 07:59:23 +00:00
|
|
|
// do not support runtime rereading
|
|
|
|
}
|
2020-07-10 14:17:51 +00:00
|
|
|
|
2022-09-28 07:59:23 +00:00
|
|
|
// SetLevelString sets the minimum logging level. Default is
|
|
|
|
// "info".
|
|
|
|
//
|
|
|
|
// Returns an error if s is not a string representation of a
|
|
|
|
// supporting logging level.
|
2021-05-11 08:54:59 +00:00
|
|
|
//
|
2022-09-28 07:59:23 +00:00
|
|
|
// Supports runtime rereading.
|
2021-05-11 08:54:59 +00:00
|
|
|
func (p *Prm) SetLevelString(s string) error {
|
|
|
|
return p.level.UnmarshalText([]byte(s))
|
|
|
|
}
|
|
|
|
|
2022-09-28 07:59:23 +00:00
|
|
|
// Reload reloads configuration of a connected instance of the Logger.
|
|
|
|
// Returns ErrLoggerNotConnected if no connection has been performed.
|
|
|
|
// Returns any reconfiguration error from the Logger directly.
|
|
|
|
func (p Prm) Reload() error {
|
|
|
|
if p._log == nil {
|
|
|
|
// incorrect logger usage
|
|
|
|
panic("parameters are not connected to any Logger")
|
|
|
|
}
|
|
|
|
|
|
|
|
return p._log.reload(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func defaultPrm() *Prm {
|
|
|
|
return new(Prm)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
// runtime reconfiguration.
|
2021-05-11 08:54:59 +00:00
|
|
|
//
|
|
|
|
// Logger is built from production logging configuration with:
|
2022-08-15 16:20:20 +00:00
|
|
|
// - parameterized level;
|
|
|
|
// - console encoding;
|
|
|
|
// - ISO8601 time encoding.
|
2021-05-11 08:54:59 +00:00
|
|
|
//
|
|
|
|
// Logger records a stack trace for all messages at or above fatal level.
|
2022-09-28 07:59:23 +00:00
|
|
|
func NewLogger(prm *Prm) (*Logger, error) {
|
|
|
|
if prm == nil {
|
|
|
|
prm = defaultPrm()
|
|
|
|
}
|
|
|
|
|
2022-09-28 07:41:01 +00:00
|
|
|
lvl := zap.NewAtomicLevelAt(prm.level)
|
|
|
|
|
2020-07-10 14:17:51 +00:00
|
|
|
c := zap.NewProductionConfig()
|
2022-09-28 07:41:01 +00:00
|
|
|
c.Level = lvl
|
2021-05-11 08:17:45 +00:00
|
|
|
c.Encoding = "console"
|
2020-07-10 14:17:51 +00:00
|
|
|
c.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
|
2023-10-08 09:32:00 +00:00
|
|
|
if prm.SamplingHook != nil {
|
|
|
|
c.Sampling.Hook = prm.SamplingHook
|
2023-06-09 08:47:39 +00:00
|
|
|
}
|
2020-07-10 14:17:51 +00:00
|
|
|
|
2022-09-28 07:59:23 +00:00
|
|
|
lZap, err := c.Build(
|
2021-05-11 08:17:45 +00:00
|
|
|
zap.AddStacktrace(zap.NewAtomicLevelAt(zap.FatalLevel)),
|
|
|
|
)
|
2022-09-28 07:41:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-09-28 07:59:23 +00:00
|
|
|
l := &Logger{Logger: lZap, lvl: lvl}
|
|
|
|
prm._log = l
|
|
|
|
|
|
|
|
return l, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Logger) reload(prm Prm) error {
|
|
|
|
l.lvl.SetLevel(prm.level)
|
|
|
|
return nil
|
2020-07-10 14:17:51 +00:00
|
|
|
}
|