Apache 的 Access log 一般都是以秒為單位,開啟詳細的 Log 格式為下述:
- LogFormat "%h %l %u %t "%r" %>s %O "%{Referer}i" "%{User-Agent}i"" combined
官方說明:Log Files - Apache HTTP Server Version 2.4
- %t 的格式:[day/month/year:hour:minute:second zone], e.g: [01/Oct/2020:13:55:36 +0800]
[10/Oct/2000:13:55:36 -0700] (%t) The time that the request was received. The format is:
[day/month/year:hour:minute:second zone]
day = 2digit
month = 3letter
year = 4digit
hour = 2digit
minute = 2digit
second = 2digit
zone = (`+' | `-') 4digit
想要將精確度到毫秒、微秒等級,要怎麼做呢?
- 註:Debian、Ubuntu Linux 設定檔:/etc/apache2/apache2.conf
Apache2 LogFormat Log 加上毫秒紀錄
Apache2 的 LogFormat 有 sec、msec、usec、msec_frac、usec_frac 等等可以使用。
官方網頁:modlogconfig - Apache HTTP Server Version 2.4
- sec:number of seconds since the Epoch
- msec:number of milliseconds since the Epoch
- usec:number of microseconds since the Epoch
- msec_frac:millisecond fraction
- usec_frac:microsecond fraction
範例
- usec=%{usec}
- usec=1582602047597083
- usec=%{usec_frac}
- usec=837628
-
usec=%{sec}t.%{usec_frac}t
- usec=1582602263.203509
官方有建議若想要把精確度提高,可以重寫 %t,改成下述:
- %t 重寫(Timestamp including milliseconds):"%{%d/%b/%Y %T}t.%{msec_frac}t %{%z}t"
- 設定檔完整範例:
LogFormat "%h %l %u %t.%{usec_frac}t "%r" %>s %O "%{Referer}i" "%{User-Agent}i" elapsed=%Dus" combined
- 25/Feb/2020 11:58:21.071 +0800 # msec_frac
- 25/Feb/2020 11:58:21.967053 +0800 # usec_frac
不想動到原始 Access Log 格式,直接往後加,可以考慮下述寫法:
- LogFormat "%h %l %u %t "%r" %>s %O "%{Referer}i" "%{User-Agent}i" elapsed=%Dus usec=%{sec}t.%{usec_frac}t" combined # sec.usec
- LogFormat "%h %l %u %t "%r" %>s %O "%{Referer}i" "%{User-Agent}i" elapsed=%Dus usec=%{usec}t" combined # 整串連在一起的