您可以同時為 FileLogWinLog 收集器設定一般記錄格式 (CLF) Apache 剖析器。

一般記錄格式 (Apache) 剖析器

預設 CLF 剖析器會定義下列欄位順序和名稱。

host ident authuser datetime request statuscode bytes

剖析器名稱:clf

CLF 剖析器特定的選項為 format

格式選項

format 選項會指定產生的 Apache 記錄所採用的格式。此選項並非強制性選項。

如果未指定格式,則系統會使用下列預設一般記錄格式。

%h %l %u %t \"%r\" %s %b

CLF 剖析器格式字串不接受 regex 運算式。例如指定空格,而非運算式 \s+。

若要剖析其他記錄格式,請在代理程式的組態中指定該格式。剖析的欄位會使用下列名稱顯示在伺服器端上。

備註: 在需要變數的情況下,如果組態中未提供 {VARNAME},欄位將會忽略。
欄位
'%a': "remote_ip"
'%A': "local_ip"
'%B', '%b': "response_size"
'%C': 取決於格式中指定的變數名稱
'%c': 取決於格式中指定的變數名稱
'%D': "request_time_mcs"
'%E': "error_status"
'%e': 取決於格式中指定的變數名稱
'%F', '%f': "file_name"
'%h': "remote_host"
'%H': "request_protocol"
'%i': 取決於格式中指定的變數名稱
'%k': "keepalive_request_count"
'%l': "remote_log_name"
'%L' "request_log_id"
'%M': "log_message" (達到此規範後,剖析器會停止剖析輸入記錄)
'%m': "request_method"
'%n': 取決於格式中指定的變數名稱
'%o': 取決於格式中指定的變數名稱
'%p': "server_port"

其他格式可與此指定名稱搭配使用:%{format}p。支援的格式為 "canonical"、"local" 或 "remote"。使用 "canonical" 格式時,欄位名稱會保留為 "server_port"。使用 "local" 格式時,欄位名稱將為 "local_server_port";而使用 "remote" 格式時,欄位名稱將為 "remote_server_port"。

'%P': "process_id"

其他格式可與此指定名稱搭配使用:%{format}P。支援的格式為 "pid"、"tid" 和 "hextid"。如果使用 "pid" 作為格式,欄位名稱將為 "process_id",而 "tid" 和 "hextid" 格式會產生名為 "thread_id" 的欄位

'%q': "query_string"
'%r': "request"
'%R': "response_handler"
'%s': "status_code",產生要求的最終狀態。
'%t':

"timestamp",作為擷取時的事件時間戳記,並結合時間戳記剖析器。若要覆寫時間戳記自動偵測,您可以在大括號中指定日期和時間格式:%{%Y-%m-%d %H:%M:%S}t;如需詳細資訊,請參閱時間戳記剖析器

CLF 剖析器的時間戳記格式開頭可以是 "begin:""end:" 前置詞。如果格式以 begin: 開頭 (預設),則系統會採用要求處理開始的時間。如果以 end: 開頭,則為寫入記錄項目的時間,這會接近要求處理結束的時間。例如,CLF 剖析器支援下列類型的格式:%h %l %u [%{begin:%d/%b/%Y %T}t.%{msec_frac}t] \"%r\" %>s %b

CLF 剖析器的時間戳記格式指定名稱也支援下列格式權杖:
sec
Epoch 之後的秒數。這等同於時間戳記剖析器的 %s 規範。
msec
Epoch 之後的毫秒數
usec
Epoch 之後的微秒數
msec_frac
毫秒分數 (等同於時間戳記剖析器的 %f 指定名稱)
musec
微秒分數 (等同於時間戳記剖析器的 %f 指定名稱)
若要剖析透過格式權杖表示時間戳記的記錄,您可以在組態中使用下列格式:
format=%h %l %u %{sec}t \"%r\" %s %b
format=%h %l %u %{msec}t \"%r\" %s %b
format=%h %l %u %{usec}t \"%r\" %s %b

這些權杖無法彼此合併,或與相同格式字串中的時間戳記剖析器格式合併。您可以改為使用多個 %{format}t 權杖。例如,若要使用包含毫秒的時間戳記,除了使用時間戳記剖析器的 %f 指定名稱以外,您也可以使用下列合併的時間戳記: %{%d/%b/%Y %T}t.%{msec_frac}t

'%T': "request_time_sec"
'%u': "remote_auth_user"
'%U': "requested_url"
'%v': "server_name"
'%V': "self_referential_server_name"
'%X': "connection_status",取決於格式中指定的變數名稱
'%x': 取決於格式中指定的變數名稱
'%I': "received_bytes"
'%O': "sent_bytes"
'%S': "transferred_size"

例如,若要使用 CLF 剖析器剖析從 winlogfilelog 來源收集的記錄,請指定下列組態:

[filelog|clflogs]
directory=D:\Logs
include=*.txt
parser=myclf

[parser|myclf]
debug=yes ;Note: use this option only while debugging and set it to ‘no’ when used in production.
base_parser=clf
format=%h %l %u %b %t \"%r\" %s

在使用此組態的情況下,從 clflogs 來源 (例如從 directory=D:\Logs 目錄) 收集的記錄將由 myclf 剖析。myclf 剖析器僅剖析採用組態中所述格式產生的記錄。

對於剖析器,偵錯預設值為 debug=no

剖析使用 CLF 產生的記錄

若要剖析使用 CLF 產生的記錄,您必須在組態中定義相應的格式。例如,

format=%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User_Agent}i\"

使用規範 %{Referer}i%{User_Agent}i 的非空欄位會顯示在 vRealize Log Insight 伺服器上,各自名稱為 refereruser_agent

整合時間戳記剖析器與 CLF 剖析器

您可以剖析包含自訂時間格式的 Apache 記錄。

存取具有如下自訂時間格式的記錄。

format = %h %l %u %{%a, %d %b %Y %H:%M:%S}t \"%r\" %>s %b

如果未指定自訂時間,CLF 剖析器會嘗試透過執行自動時間戳記剖析器自動推斷時間格式,否則將使用自訂時間格式。

錯誤記錄支援的自訂時間格式為:

自訂時間格式 說明 組態格式
%{u}t 目前時間,包括微秒 format=[%{u}t] [%l] [pid %P] [client %a] %M
%{cu}t 採用精簡 ISO 8601 格式的目前時間,包括微秒 format=[%{cu}t] [%l] [pid %P] [client %a] %M

如需支援的時間戳記規範的完整清單,請參閱時間戳記剖析器

適用於 Windows 的 Apache 預設存取記錄組態

此範例介紹如何設定適用於 Windows 的 Apache v2.4 存取記錄組態的格式。

;ACCESS LOG
;127.0.0.1 - - [13/May/2015:14:44:05 +0400] "GET /xampp/navi.php HTTP/1.1" 200 4023 "http://localhost/xampp/" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:37.0) Gecko/20100101 Firefox/37.0"
;format=%h %l %u %{%d/%b/%Y:%H:%M:%S %z}t \"%r\" %>s %b \"%{Referer}i\" \"%{User_agent}i\"
 
; Section to collect Apache ACCESS logs
[filelog|clflogs-access]
    directory=C:\xampp\apache\logs
    include=acc*
    parser=clfparser_apache_access
    enabled=yes

;Parser to parse Apache ACCESS logs
[parser|clfparser_apache_access]
    debug=yes
    base_parser=clf
    format=%h %l %u %{%d/%b/%Y:%H:%M:%S %z}t \"%r\" %>s %b \"%{Referer}i\" \"%{User_agent}i\"
定義存取記錄格式:
  1. 為存取記錄格式 (httpd.conf) 設定 Apache:
     LogFormat "%h %l %u %{%d-%b-%Y:%H:%M:%S}t \"%r\" %a %A %e %k %l %L %m %n %T %v %V %>s %b \"%{Referer}i\" \"%{User_Agent}i\"" combined
    
  2. 定義 CLF 剖析器組態:
;ACCESS LOG
;127.0.0.1 unknown - 21-May-2015:13:59:35 "GET /xampp/navi.php HTTP/1.1" 127.0.0.1 127.0.0.1 - 0 unknown - GET - 1 localhost localhost 200 4023 "http://localhost/xampp/" "-"
[filelog|clflogs-access]
    directory=C:\xampp\apache\logs
    include=acc*;_myAcc*
    parser=clfparser_apache_access
    enabled=yes
; Parser to parse Apache ACCESS logs
[parser|clfparser_apache_access]
   debug=yes
   base_parser=clf
   format=%h %l %u %{%d-%b-%Y:%H:%M:%S}t \"%r\" %a %A %e %k %l %L %m %n %T %v %V %>s %b \"%{Referer}i\" \"%{User_Agent}i\"
CLF 剖析器會傳回以下內容:
remote_host=127.0.0.1
timestamp=2015-05-13T10:44:05
request=GET /xampp/navi.php HTTP/1.1
status_code=200
response_size=4023
referer=http://localhost/xampp/
user_agent=Mozilla/5.0 (Windows NT 6.1; WOW64; rv:37.0) Gecko/20100101 Firefox/37.0

此範例介紹如何設定適用於 Windows 的 Apache v2.4 錯誤記錄組態的格式。

;ERROR LOG
;[Wed May 13 14:37:17.042371 2015] [mpm_winnt:notice] [pid 4488:tid 272] AH00354: Child: Starting 150 worker threads.
;[Wed May 13 14:37:27.042371 2015] [mpm_winnt:notice] [pid 5288] AH00418: Parent: Created child process 3480
;format=[%{%a %b %d %H:%M:%S%f %Y}t] [%m:%{severity}i] [pid %P:tid %{thread_id}i] %E: %M
;format=[%{%a %b %d %H:%M:%S%f %Y}t] [%m:%{severity}i] [pid %P] %E: %M 
 
; Section to collect Apache ERROR logs
[filelog|clflogs-error]
    directory=C:\xampp\apache\logs
    include=err*
    parser=clfparser_apache_error
    enabled=yes
 
;Parser to parse Apache ERROR logs
[parser|clfparser_apache_error]
    debug=yes
    base_parser=clf
    format=[%{%a %b %d %H:%M:%S%f %Y}t] [%m:%{severity}i] [pid %P:tid %{thread_id}i] %E: %M
    next_parser=clfparser_apache_error2
 
;Parser to parse Apache ERROR logs
[parser|clfparser_apache_error2]
    debug=yes
    base_parser=clf
    format=[%{%a %b %d %H:%M:%S%f %Y}t] [%m:%{severity}i] [pid %P] %E: %M
備註: 提供的名稱和合併的記錄格式相對應。此外,Apache 錯誤記錄還使用上述格式設定索引鍵加以說明,而非 Apache 錯誤記錄格式。
定義錯誤記錄格式:
  1. 為錯誤記錄格式 (httpd.conf) 設定 Apache:
     LogFormat "%h %l %u %{%d-%b-%Y:%H:%M:%S}t \"%r\" %a %A %e %k %l %L %m %n %T %v %V %>s %b \"%{Referer}i\" \"%{User_Agent}i\"" combined
    
  2. 定義 CLF 剖析器組態:
;Parser to parse Apache ERROR logs
[parser|clfparser_apache_error]
   debug=yes
   base_parser=clf
   format=[%{%a %b %d %H:%M:%S%f %Y}t] [%m:%{severity}i] [pid %P] %E: %M
   next_parser=clfparser_apache_error2

;Parser to parse Apache ERROR logs
[parser|clfparser_apache_error2]
   debug=yes
   base_parser=clf
   format=[%{%a %b %d %H:%M:%S%f %Y}t] [%m:%{severity}i] [pid %P:tid %{thread_id}i] %E: %M

記錄項目:

[Wed May 13 14:37:17.042371 2015] [mpm_winnt:notice] [pid 4488:tid 272] AH00354: Child: Starting 150 worker threads.
CLF 剖析器會傳回記錄項目的以下欄位 (若在 +0400 時區中使用剖析器):
timestamp=2015-05-13T10:37:17.042371
request_method=mpm_winnt
severity=notice
process_id=4488
thread_id=272
error_status=AH00354
log_message=Child: Starting 150 worker threads.

記錄項目:

[Wed May 13 14:37:27.042371 2015] [mpm_winnt:notice] [pid 5288] AH00418: Parent: Created child process 3480
CLF 剖析器會傳回記錄項目的以下欄位 (若在 +0400 時區中使用剖析器):
timestamp=2015-05-13T10:37:27.042371
request_method=mpm_winnt
severity=notice
process_id=5288
error_status=AH00418
log_message=Parent: Created child process 3480