From 9e1d964bd6af27cd96a370987a1cadc58933d19a Mon Sep 17 00:00:00 2001 From: Francis Lavoie Date: Wed, 5 Oct 2022 00:23:14 -0400 Subject: logging: Add `time_local` option to use local time instead of UTC (#5108) --- modules/logging/encoders.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'modules/logging') diff --git a/modules/logging/encoders.go b/modules/logging/encoders.go index d8825d3..1cfab8e 100644 --- a/modules/logging/encoders.go +++ b/modules/logging/encoders.go @@ -127,6 +127,7 @@ type LogEncoderConfig struct { StacktraceKey *string `json:"stacktrace_key,omitempty"` LineEnding *string `json:"line_ending,omitempty"` TimeFormat string `json:"time_format,omitempty"` + TimeLocal bool `json:"time_local,omitempty"` DurationFormat string `json:"duration_format,omitempty"` LevelFormat string `json:"level_format,omitempty"` } @@ -142,12 +143,21 @@ type LogEncoderConfig struct { // stacktrace_key // line_ending // time_format +// time_local // duration_format // level_format // } func (lec *LogEncoderConfig) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { for nesting := d.Nesting(); d.NextBlock(nesting); { subdir := d.Val() + switch subdir { + case "time_local": + lec.TimeLocal = true + if d.NextArg() { + return d.ArgErr() + } + continue + } var arg string if !d.AllArgs(&arg) { return d.ArgErr() @@ -237,7 +247,13 @@ func (lec *LogEncoderConfig) ZapcoreEncoderConfig() zapcore.EncoderConfig { timeFormat = "02/Jan/2006:15:04:05 -0700" } timeFormatter = func(ts time.Time, encoder zapcore.PrimitiveArrayEncoder) { - encoder.AppendString(ts.UTC().Format(timeFormat)) + var time time.Time + if lec.TimeLocal { + time = ts.Local() + } else { + time = ts.UTC() + } + encoder.AppendString(time.Format(timeFormat)) } } cfg.EncodeTime = timeFormatter -- cgit v1.2.3