大网数据广告活动
服务器运维问答

How to change the timestamp format in error logs?

· 52CE 我爱测

I'd like to have the timestamps in my Nginx error logs the same as in my access logs. The error times have the format Y/m/d H:i:s whereas the access logs allow me either: d/M/Y:H:i:s O ($time_local) or Y-m-d\TH:i:sO ($time_iso860…

问题原文

原帖链接 · Tim · CC BY-SA 3.0

I'd like to have the timestamps in my Nginx error logs the same as in my access logs.

The error times have the format Y/m/d H:i:s whereas the access logs allow me either:

  • d/M/Y:H:i:s O ($time_local) or
  • Y-m-d\TH:i:sO ($time_iso8601)

Is there any way I can set my access logs to use the error log format or vice versa?

I'm running Nginx 1.10.1 on CentOS 7.


Update:

As my question has been downvoted, I shall try to be crystal clear -

The access_log directive allows me to define the timestamp used in access logs, although it seems I only have two choices of format. for example:

log_format foo '[$time_local] "$request" $status ..';
log_format bar '[$time_iso8601] "$request" $status ..';

I can then apply these formats to access logs like this:

access_log foo.log foo;
access_log bar.log bar;

Tailing all the logs shows the different time formats, e.g.

==> foo.log <==
[29/Sep/2016:10:20:48 +0100] "GET /fail HTTP/1.1" 404 ..

==> bar.log <==
[2016-09-29T10:20:48+01:00] "GET /fail HTTP/1.1" 404 ..

==> error_log <==
2016/09/29 10:37:52 [error] .....  No such file or directory

Neither $time_local nor $time_iso8601 match the format used in the standard Nginx error log. I have tried to solve this in two ways:

  1. Configure error logging to use either $time_locale or $time_iso8601.
    ~ I have failed to find any directive that can do this.
  2. Configure the access log time format to match the error log time format.
    ~ I have failed to find any variables that can do this.

I am not married to any particular time format, but I would like them to match in my error logs as it saves me time when cross referencing access and error log entries.

If anyone knows how to achieve either 1. or 2. above, I'd love to know. Thanks.

社区高分解答

原帖链接 · Ryan · CC BY-SA 4.0

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.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-09-28)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。