2020-07-10 14:17:51 +00:00
|
|
|
package logger
|
|
|
|
|
|
|
|
import (
|
2024-02-08 14:04:58 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"git.frostfs.info/TrueCloudLab/zapjournald"
|
|
|
|
"github.com/ssgreg/journald"
|
2020-07-10 14:17:51 +00:00
|
|
|
"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
|
2024-02-08 14:04:58 +00:00
|
|
|
dest string
|
2022-09-28 07:59:23 +00:00
|
|
|
}
|
2020-07-10 14:17:51 +00:00
|
|
|
|
2024-02-08 14:04:58 +00:00
|
|
|
const (
|
|
|
|
DestinationUndefined = ""
|
|
|
|
DestinationStdout = "stdout"
|
|
|
|
DestinationJournald = "journald"
|
|
|
|
)
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2024-02-08 14:04:58 +00:00
|
|
|
func (p *Prm) SetDestination(d string) error {
|
|
|
|
if d != DestinationStdout && d != DestinationJournald {
|
|
|
|
return fmt.Errorf("invalid logger destination %s", d)
|
|
|
|
}
|
|
|
|
if p != nil {
|
|
|
|
p.dest = d
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
}
|
2024-02-08 14:04:58 +00:00
|
|
|
switch prm.dest {
|
|
|
|
case DestinationUndefined, DestinationStdout:
|
|
|
|
return newConsoleLogger(prm)
|
|
|
|
case DestinationJournald:
|
|
|
|
return newJournaldLogger(prm)
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unknown destination %s", prm.dest)
|
|
|
|
}
|
|
|
|
}
|
2022-09-28 07:59:23 +00:00
|
|
|
|
2024-02-08 14:04:58 +00:00
|
|
|
func newConsoleLogger(prm *Prm) (*Logger, error) {
|
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
|
|
|
|
}
|
|
|
|
|
2024-02-08 14:04:58 +00:00
|
|
|
func newJournaldLogger(prm *Prm) (*Logger, error) {
|
|
|
|
lvl := zap.NewAtomicLevelAt(prm.level)
|
|
|
|
|
|
|
|
c := zap.NewProductionConfig()
|
|
|
|
c.Level = lvl
|
|
|
|
c.Encoding = "console"
|
|
|
|
c.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
|
|
|
|
if prm.SamplingHook != nil {
|
|
|
|
c.Sampling.Hook = prm.SamplingHook
|
|
|
|
}
|
|
|
|
|
|
|
|
encoder := zapjournald.NewPartialEncoder(zapcore.NewConsoleEncoder(c.EncoderConfig), zapjournald.SyslogFields)
|
|
|
|
|
2024-02-19 10:08:18 +00:00
|
|
|
core := zapjournald.NewCore(lvl, encoder, &journald.Journal{}, zapjournald.SyslogFields)
|
2024-02-08 14:04:58 +00:00
|
|
|
coreWithContext := core.With([]zapcore.Field{
|
|
|
|
zapjournald.SyslogFacility(zapjournald.LogDaemon),
|
|
|
|
zapjournald.SyslogIdentifier(),
|
|
|
|
zapjournald.SyslogPid(),
|
|
|
|
})
|
|
|
|
|
|
|
|
lZap := zap.New(coreWithContext, zap.AddStacktrace(zap.NewAtomicLevelAt(zap.FatalLevel)))
|
|
|
|
|
|
|
|
l := &Logger{Logger: lZap, lvl: lvl}
|
|
|
|
prm._log = l
|
|
|
|
|
|
|
|
return l, nil
|
|
|
|
}
|
|
|
|
|
2022-09-28 07:59:23 +00:00
|
|
|
func (l *Logger) reload(prm Prm) error {
|
|
|
|
l.lvl.SetLevel(prm.level)
|
|
|
|
return nil
|
2020-07-10 14:17:51 +00:00
|
|
|
}
|