作者 主題: 分享:如何自訂 Apache Log  (閱讀 9913 次)

0 會員 與 1 訪客 正在閱讀本文。

wbyron

  • 懷疑的國中生
  • **
  • 文章數: 81
  • 性別: 男
    • 檢視個人資料
分享:如何自訂 Apache Log
« 於: 2009-10-11 01:12 »
每當在看 apache 的 log 檔時,總是覺得畫面密密麻麻的,好多字都看不完,雖然預設的 log 已經提供足夠的訊息讓我們來偵錯或查詢異狀,但總是覺得不夠人性化,也許您會覺得看多了就會習慣,可是換個角度想,如果可以按照自己想要的訊息來紀錄,不是更好嗎,所以才會有這篇文章。

話不多說,先來看以下的資料 (資料來源:http://httpd.apache.org/docs/2.2/mod/mod_log_config.html#formats)

Apache Custom Log Formats (如果翻譯的不對,請糾正。)

Format StringDescription
%%百分比符號
%a遠端 IP 位址
%A本機 IP 位址
%B回應一個 request 的大小 (bytes),含 HTTP headers
%b以 CLF 格式回應一個 request 的大小 (bytes),含 HTTP headers
%{Foobar}C傳送到伺服器的 Foobar cookie 的內容
%Drequest 傳送到伺服器的時間 (microseconds)
%{FOOBAR}e伺服器環境變數 FOOBAR 的內容
%f檔名
%h遠端主機名稱
%Hrequest 的通訊協定
%{Foobar}iThe contents of Foobar: header line(s) in the request sent to the server.
Changes made by other modules (e.g. mod_headers) affect this.
%k在一個 connection requests handled 中 keepalive 的數量
%l遠端的 logname (This will return a dash unless mod_ident is present and Identity Check is set On).
%mThe request method.
%{Foobar}nThe contents of note Foobar from another module.
%{Foobar}oThe contents of Foobar: header line(s) in the reply.
%p伺服器回應一個 request 的 canonical port number
%{format}pThe canonical port of the server serving the request or the server's actual port or the client's actual port.
Valid formats are canonical, local, or remote.
%P回應 request 時的 PID
%{format}PThe process ID or thread id of the child that serviced the request. Valid formats are pid, tid, and hextid. hextid requires APR 1.2.0 or higher.
%qThe query string (prepended with a ? if a query string exists, otherwise an empty string).
%rFirst line of request.
%s狀態. 若是由內部轉址來的 request,則將顯示原始 request 的狀態
%t伺服器收到 request 的時間 (standard english format)
%{format}t同上,但以自訂的格式來顯示時間
%T伺服器回應 request 的時間 (in seconds)
%u遠端的使用者 (from auth)
%UThe URL path requested (not including any query string)
%v伺服器回應 request 時的 canonical ServerName
%VThe server name according to the UseCanonicalName setting.
%XConnection status when response is completed :
 X:connection aborted before the response completed.
 +:connection may be kept alive after the response is sent.
 -:connection will be closed after the response is sent.
This directive was %c in late versions of Apache 1.3, but this conflicted with the historical ssl %{var}c syntax.
%IBytes received, including request and headers, cannot be zero. You need to enable mod_logio to use this.
%OBytes sent, including headers, cannot be zero. You need to enable mod_logio to use this.

我的 httpd.conf 中 log 的設定:

引用
LogFormat "--------------------\n%{%Y-%m-%d}t%{%H:%M:%S}t from %h\nrequest url : %U\nquery string : %q\nstatus : %s\n%{User-Agent}i" localDefine

 # \n:代表換行
 # {%Y-%m-%d}t:代表將時間以 日期 來顯示
 # %{%H:%M:%S}:代表將時間以 時:分:秒 來顯示

‥‥
CustomLog "/var/log/httpd-access.log" localDefine

套用設定所顯示的效果:

引用
--------------------
2009-10-11 00:37:41 from 163.19.150.208
request url : /albumThumb.php
query string : ?s=35&i=recorderTeam-03.jpg&pr=/webhd/share/photo&pd=2009-09-25-Recorder_Concert
status : 200
Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-TW; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3
--------------------
2009-10-11 00:37:41 from 163.19.150.208
request url : /albumThumb.php
query string : ?s=35&i=listenStory-20.jpg&pr=/webhd/share/photo&pd=2009-08-29-Listen_Story
status : 200
Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-TW; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3
--------------------
2009-10-11 00:37:41 from 163.19.150.208
request url : /albumThumb.php
query string : ?s=35&i=leofoo-19.jpg&pr=/webhd/share/photo&pd=2009-09-19-LeoFoo_Village
status : 200
Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-TW; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3
‥‥
« 上次編輯: 2009-10-11 01:15 由 wbyron »

Yamaka

  • 俺是博士!
  • *****
  • 文章數: 4913
    • 檢視個人資料
    • http://www.ecmagic.com
回覆: 分享:如何自訂 Apache Log
« 回覆 #1 於: 2009-10-11 08:48 »
感謝分享~~  ^^

對人眼來說, 真的會輕鬆許多
只是如果有用到script分析log的話
可能就要連scrtip一起改了  :D