Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

logfile can not output localtime #5019

Closed
cnkmmk opened this issue Sep 6, 2022 · 8 comments · Fixed by #5108
Closed

logfile can not output localtime #5019

cnkmmk opened this issue Sep 6, 2022 · 8 comments · Fixed by #5108
Labels
feature ⚙️ New feature or request
Milestone

Comments

@cnkmmk
Copy link

cnkmmk commented Sep 6, 2022

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}
@mholt
Copy link
Member

mholt commented Sep 7, 2022

(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 mholt closed this as completed Sep 7, 2022
@mholt mholt added the plugin 🔌 A feature outside this repo label Sep 7, 2022
@francislavoie
Copy link
Member

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.

@mholt
Copy link
Member

mholt commented Sep 7, 2022

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

@cnkmmk
Copy link
Author

cnkmmk commented Sep 8, 2022

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

@mholt
Copy link
Member

mholt commented Sep 8, 2022

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

@cnkmmk
Copy link
Author

cnkmmk commented Sep 9, 2022

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

@cnkmmk
Copy link
Author

cnkmmk commented Sep 9, 2022

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

@mholt mholt added feature ⚙️ New feature or request and removed plugin 🔌 A feature outside this repo labels Sep 9, 2022
@mholt mholt reopened this Sep 9, 2022
@mholt mholt added this to the v2.7.0 milestone Sep 19, 2022
@francislavoie
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature ⚙️ New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants