Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 13, 09:15 UTC
logging: Add `time_local` option to use local time instead of UTC (#5108)
Francis Lavoie committed
Oct 5, 2022 at 00:23 UTC
9e1d964bd6af27cd96a370987a1cadc58933d19a
1 file changed
+17
-1
modules/logging/encoders.go
+17
-1
@@ -127,6 +127,7 @@ type LogEncoderConfig struct {
127
StacktraceKey *string `json:"stacktrace_key,omitempty"`
128
LineEnding *string `json:"line_ending,omitempty"`
129
TimeFormat string `json:"time_format,omitempty"`
130
+ TimeLocal bool `json:"time_local,omitempty"`
131
DurationFormat string `json:"duration_format,omitempty"`
132
LevelFormat string `json:"level_format,omitempty"`
133
}
@@ -142,12 +143,21 @@ type LogEncoderConfig struct {
143
// stacktrace_key <key>
144
// line_ending <char>
145
// time_format <format>
146
+// time_local
147
// duration_format <format>
148
// level_format <format>
149
// }
150
func (lec *LogEncoderConfig) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
151
for nesting := d.Nesting(); d.NextBlock(nesting); {
152
subdir := d.Val()
153
+ switch subdir {
154
+ case "time_local":
155
+ lec.TimeLocal = true
156
+ if d.NextArg() {
157
+ return d.ArgErr()
158
+ }
159
+ continue
160
+ }
161
var arg string
162
if !d.AllArgs(&arg) {
163
return d.ArgErr()
@@ -237,7 +247,13 @@ func (lec *LogEncoderConfig) ZapcoreEncoderConfig() zapcore.EncoderConfig {
247
timeFormat = "02/Jan/2006:15:04:05 -0700"
248
}
249
timeFormatter = func(ts time.Time, encoder zapcore.PrimitiveArrayEncoder) {
240
- encoder.AppendString(ts.UTC().Format(timeFormat))
250
+ var time time.Time
251
+ if lec.TimeLocal {
252
+ time = ts.Local()
253
+ } else {
254
+ time = ts.UTC()
255
+ }
256
+ encoder.AppendString(time.Format(timeFormat))
257
}
258
}
259
cfg.EncodeTime = timeFormatter