caddy
caddy copied to clipboard
logfile can not output localtime
hi guys, I can not output the localtime in the caddy log file. the TS alway display UTC. the server timezone is set,
Local time: Tue 2022-09-06 15:45:55 CST
Universal time: Tue 2022-09-06 07:45:55 UTC
RTC time: Tue 2022-09-06 07:45:56
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no
the log content
{"level":"info","ts":"2022/09/06 07:51:40","logger":"http.log.access.log0","msg":"handled request","request":{"remote_addr":"127.0.0.1:55063","uri":"/reflesh/"},"status":204}
(I'm not sure why the docs aren't rendering for this property, but) you can customize the time format: https://caddyserver.com/docs/json/logging/logs/encoder/json/time_format/
See https://caddyserver.com/docs/caddyfile/directives/log#format-modules for the supported time format options. I'm not sure if any of those actually use the timezone though, it might just always be UTC.
Those are just presets. You can use any Go-supported time format, which lets you choose the time zone: https://pkg.go.dev/time#pkg-constants
I don't study Golang. I checked the source code, find this in "encoders.go"
// time format
var timeFormatter zapcore.TimeEncoder
switch lec.TimeFormat {
case "", "unix_seconds_float":
timeFormatter = zapcore.EpochTimeEncoder
case "unix_milli_float":
timeFormatter = zapcore.EpochMillisTimeEncoder
case "unix_nano":
timeFormatter = zapcore.EpochNanosTimeEncoder
case "iso8601":
timeFormatter = zapcore.ISO8601TimeEncoder
default:
timeFormat := lec.TimeFormat
switch lec.TimeFormat {
case "rfc3339":
timeFormat = time.RFC3339
case "rfc3339_nano":
timeFormat = time.RFC3339Nano
case "wall":
timeFormat = "2006/01/02 15:04:05"
case "wall_milli":
timeFormat = "2006/01/02 15:04:05.000"
case "wall_nano":
timeFormat = "2006/01/02 15:04:05.000000000"
case "common_log":
timeFormat = "02/Jan/2006:15:04:05 -0700"
}
timeFormatter = func(ts time.Time, encoder zapcore.PrimitiveArrayEncoder) {
encoder.AppendString(ts.UTC().Format(timeFormat))
}
}
cfg.EncodeTime = timeFormatter
Yep, so it's what we told you :+1: Just put in a format that works for you. (Link above describes the format.)
sorry, I mean that the time is UTC in logfile, I want Local.
func (Time) Local
func (t Time) Local() Time
Local returns t with the location set to local time.
func (Time) UTC
func (t Time) UTC() Time
UTC returns t with the location set to UTC.
I can't set the time is Local in log file, I guess maybe in this
encoder.AppendString(ts.UTC().Format(timeFormat))
FYI @cnkmmk I opened #5108 which adds a new option to use local time instead of UTC