《Disney +》 迪士尼、Marvel、彼思、星球大戰…  《Among Us》官方授權週邊確定發售!船員們變成紓壓小物!   解謎類遊戲「地城謎蹤」於Steam開始販售!絞盡腦汁取得最佳分數吧!   Bandai Namco發表新LOGO 未來將轉用新的標誌   「Fight of Animals: Arena」新動物參戰!兩腳行走毛茸茸的那傢伙來了!   FINAL FANTASY VII THE FIRST SOLDIER 上架!賽季開幕!   在遊戲中心「太鼓之達人」與「AI粗品」對戰吧!「AI粗品對戰演奏」模式啟動!   PlayStation Store黑色星期五大特價!新作都有優惠   六月份PlayStation Plus遊戲:《行動代號:探戈》、《Virtua Fighter 5: Ultimate Showdown》、《Star Wars:中隊爭雄》 

Apache2 LogFormat Log 加上毫秒紀錄

商業

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 # 整串連在一起的
Tsung

隨機商業新聞