How to change the timestamp format in error logs?

Solution 1:

Sadly it seems that it's not possible to change the error_log format, at least according to this and this.

It also doesn't seem possible to change the format of the time field in access logs either, so I think you're out of luck with a purely NGINX solution.

That said, it should be possible to send them both to syslog and hope that it uses the same format, or at least gives you better options.

Solution 2:

https://thatsamguy.com/nginx-iso8601-time-format/

As @GregL stated its not possible to change the format of the error logs, but using maps you should be able to create a timestamp for the access logs which matches the error logs. I personally have modified our access logs to include milliseconds as shown in the link above.

You can break up the time_iso8601 into 2 parts, the date and time.

map $time_iso8601 $date {
  ~([^T]+) $1;
}
map $time_iso8601 $time {
  ~\T([0-9:]+)\+ $1;
}

Then just reference them in your log_format as desired.