[#1366] logger: Make timestamp prepending optional

Signed-off-by: Aleksey Savchuk <a.savchuk@yadro.com>
This commit is contained in:
Aleksey Savchuk 2024-09-16 10:45:26 +03:00
parent 96308a26c6
commit ea48e928c8
No known key found for this signature in database

View file

@ -40,6 +40,9 @@ type Prm struct {
// do not support runtime rereading // do not support runtime rereading
dest string dest string
// PrependTimestamp specifies whether to prepend a timestamp in the log
PrependTimestamp bool
} }
const ( const (
@ -116,11 +119,16 @@ func newConsoleLogger(prm *Prm) (*Logger, error) {
c := zap.NewProductionConfig() c := zap.NewProductionConfig()
c.Level = lvl c.Level = lvl
c.Encoding = "console" c.Encoding = "console"
c.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
if prm.SamplingHook != nil { if prm.SamplingHook != nil {
c.Sampling.Hook = prm.SamplingHook c.Sampling.Hook = prm.SamplingHook
} }
if prm.PrependTimestamp {
c.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
} else {
c.EncoderConfig.TimeKey = ""
}
lZap, err := c.Build( lZap, err := c.Build(
zap.AddStacktrace(zap.NewAtomicLevelAt(zap.FatalLevel)), zap.AddStacktrace(zap.NewAtomicLevelAt(zap.FatalLevel)),
) )
@ -140,11 +148,16 @@ func newJournaldLogger(prm *Prm) (*Logger, error) {
c := zap.NewProductionConfig() c := zap.NewProductionConfig()
c.Level = lvl c.Level = lvl
c.Encoding = "console" c.Encoding = "console"
c.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
if prm.SamplingHook != nil { if prm.SamplingHook != nil {
c.Sampling.Hook = prm.SamplingHook c.Sampling.Hook = prm.SamplingHook
} }
if prm.PrependTimestamp {
c.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
} else {
c.EncoderConfig.TimeKey = ""
}
encoder := zapjournald.NewPartialEncoder(zapcore.NewConsoleEncoder(c.EncoderConfig), zapjournald.SyslogFields) encoder := zapjournald.NewPartialEncoder(zapcore.NewConsoleEncoder(c.EncoderConfig), zapjournald.SyslogFields)
core := zapjournald.NewCore(lvl, encoder, &journald.Journal{}, zapjournald.SyslogFields) core := zapjournald.NewCore(lvl, encoder, &journald.Journal{}, zapjournald.SyslogFields)