caddy icon indicating copy to clipboard operation
caddy copied to clipboard

logfile can not output localtime

Open cnkmmk opened this issue 2 years ago • 7 comments

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}

cnkmmk avatar Sep 06 '22 07:09 cnkmmk

(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/

mholt avatar Sep 07 '22 18:09 mholt

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.

francislavoie avatar Sep 07 '22 18:09 francislavoie

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

mholt avatar Sep 07 '22 18:09 mholt

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

cnkmmk avatar Sep 08 '22 16:09 cnkmmk

Yep, so it's what we told you :+1: Just put in a format that works for you. (Link above describes the format.)

mholt avatar Sep 08 '22 16:09 mholt

sorry, I mean that the time is UTC in logfile, I want Local.

cnkmmk avatar Sep 09 '22 15:09 cnkmmk

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))

cnkmmk avatar Sep 09 '22 16:09 cnkmmk

FYI @cnkmmk I opened #5108 which adds a new option to use local time instead of UTC

francislavoie avatar Oct 02 '22 20:10 francislavoie