Apache日志格式


Apache日志格式分類

apache中日志記錄格式主要有兩種,普通型(common)和復合型(combined),安裝時默認使用普通型(common)類型日志記錄訪問信息,
LogFormat指令:定義格式並為格式指定一個名字,以后我們就可以直接引用這個名字
CustomLog指令:設置日志文件,並指明日志文件所用的格式(通常通過格式的名字)
ErrorLog指令:錯誤日志的位置可以通過ErrorLog指令設置:ErrorLog logs/error.log  # 錯誤日志無論在格式上還是在內容上都和訪問日志不同

默認Apache訪問日志格式

主配置文件里提供了兩種日志的模板:common和combined
<IfModule log_config_module>
    #
    # The following directives define some format nicknames for use with
    # a CustomLog directive (see below).
    #
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common

    <IfModule logio_module>
      # You need to enable mod_logio.c to use %I and %O
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
    </IfModule>

    #
    # The location and format of the access logfile (Common Logfile Format).
    # If you do not define any access logfiles within a <VirtualHost>
    # container, they will be logged here.  Contrariwise, if you *do*
    # define per-<VirtualHost> access logfiles, transactions will be
    # logged therein and *not* in this file.
    #
    # 主配置文件里預設日志模板是common
    CustomLog "logs/access_log" common

    #
    # If you prefer a logfile with access, agent, and referer information
    # (Combined Logfile Format) you can use the following directive.
    #
    #CustomLog "logs/access_log" combined
</IfModule>


默認格式common記錄樣例如下:

192.168.1.1 - - [17/Mar/2022:20:09:47 +0800] "GET /img/horse.ico HTTP/1.1" 200 4286
192.168.1.1 - - [17/Mar/2022:20:09:52 +0800] "GET /         HTTP/1.1" 304 -

1.遠程主機IP
2.空白(E-mail)
3.空白(登錄名)
4.請求時間
5.方法+資源+協議 如:GET index.html HTTP/1.1
6.HTTP狀態代碼
7.發送字節數:表示發送給客戶端的總字節數

Apache日志格式參數詳解

log_config_module
請求本身的情況將通過在格式字符串中放置各種"%"轉義符的方法來記錄,它們在寫入日志文件時,根據下表的定義進行轉換:
%a 遠端IP地址
%A 本機IP地址
%B 除HTTP頭以外傳送的字節數
%b 以CLF格式顯示的除HTTP頭以外傳送的字節數,也就是當沒有字節傳送時顯示’-'而不是0。
%{Foobar}C 在請求中傳送給服務端的cookieFoobar的內容。
%D 服務器處理本請求所用時間,以微為單位。
%{FOOBAR}e 環境變量FOOBAR的值
%f 文件名
%h 遠端主機
%H 請求使用的協議
%{Foobar}i 發送到服務器的請求頭Foobar:的內容。
%l 遠端登錄名(由identd而來,如果支持的話),除非IdentityCheck設為"On",否則將得到一個"-"%m 請求的方法
%{Foobar}n 來自另一個模塊的注解Foobar的內容。
%{Foobar}o 應答頭Foobar:的內容。
%p 服務器服務於該請求的標准端口。
%P 為本請求提供服務的子進程的PID。
%{format}P 服務於該請求的PID或TID(線程ID),format的取值范圍為:pid和tid(2.0.46及以后版本)以及hextid(需要APR1.2.0及以上版本)
%q 查詢字符串(若存在則由一個"?"引導,否則返回空串)
%r 請求的第一行
%s 狀態。對於內部重定向的請求,這個狀態指的是原始請求的狀態,—%>s則指的是最后請求的狀態。
%t 時間,用普通日志時間格式(標准英語格式)
%{format}t 時間,用strftime(3)指定的格式表示的時間。(默認情況下按本地化格式)
%T 處理完請求所花時間,以秒為單位。
%u 遠程用戶名(根據驗證信息而來;如果返回status(%s)為401,可能是假的)
%U 請求的URL路徑,不包含查詢字符串。
%v 對該請求提供服務的標准ServerName。
%V 根據UseCanonicalName指令設定的服務器名稱。
%X 請求完成時的連接狀態:
    X= 連接在應答完成前中斷。
    += 應答傳送完后繼續保持連接。
    -= 應答傳送完后關閉連接。
(在1.3以后的版本中,這個指令是%c,但這樣就和過去的SSL語法:%{var}c沖突了)
%I 接收的字節數,包括請求頭的數據,並且不能為零。要使用這個指令你必須啟用mod_logio模塊。
%O 發送的字節數,包括請求頭的數據,並且不能為零。要使用這個指令你必須啟用mod_logio模塊。


修飾符 可以緊跟在"%"后面加上一個逗號分隔的狀態碼列表來限制記錄的條目。例如,"%400,501{User-agent}i" 只記錄狀態碼400和501發生時的User-agent頭內容;不滿足條件時用"-"代替。
狀態碼前還可以加上"!"前綴表示否定,"%!200,304,302{Referer}i"記錄所有不同於200,304,302的狀態碼發生時的Referer頭內容。
"<"">"修飾符可以用來指定對於已被內部重定向的請求是選擇原始的請求還是選擇最終的請求。默認情況下,%s, %U, %T, %D, %r 使用原始請求,而所有其他格式串則選擇最終請求。
例如,%>s 可以用於記錄請求的最終狀態,而 %<u 則記錄一個已經被內部重定向到非認證資源的請求的原始認證用戶。
特殊說明
出於安全考慮,從2.0.46版本開始,%r, %i, %o 中的特殊字符,除了雙引號(")和反斜線(\)分別用 \" 和 \\ 進行轉義、空白字符用C風格(\n, \t 等)進行轉義以外,非打印字符和其它特殊字符使用 \xhh 格式進行轉義(hh是該字符的16進制編碼)。
在2.0.46以前的版本中,這些內容會被完整的按原樣記錄。這種做法將導致客戶端可以在日志中插入控制字 符,所以你在處理這些日志文件的時候要特別小心。 在2.0版本中(不同於1.3),%b 和 %B 格式字符串並不表示發送到客戶端的字節數,而只是簡單的表示HTTP應答字節數(在連接中斷或使用SSL時與前者有所不同)。mod_logio提供的 %O 格式字符串將會記錄發送的實際字節數。

我們根據上方的定義,可以自由的組合排序,從而得到我們想要的日志格式,下面為大家整理幾個常用的日志格式

通用日志格式(CLF)
"%h %l %u %t \"%r\" %>s %b"
虛擬主機的通用日志格式
"%v %h %l %u %t \"%r\" %>s %b"
NCSA擴展/組合日志格式
"%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\""
Referer日志格式
"%{Referer}i -> %U"
Agent(Browser)日志格式
"%{User-agent}i"
常用版本
"%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\""

如何自定義日志格式

[root@web conf]# vim /usr/local/apache/conf/httpd.conf
<IfModule log_config_module>
    #
    # The following directives define some format nicknames for use with
    # a CustomLog directive (see below).
    #
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common
    # 自定義日志格式
    LogFormat "%h  %l  %u  %t  \"%r\"  %>s  %b  \"%{Referer}i\"  \"%{User-Agent}i\"" selfcommon

    <IfModule logio_module>
      # You need to enable mod_logio.c to use %I and %O
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
    </IfModule>

    #
    # The location and format of the access logfile (Common Logfile Format).
    # If you do not define any access logfiles within a <VirtualHost>
    # container, they will be logged here.  Contrariwise, if you *do*
    # define per-<VirtualHost> access logfiles, transactions will be
    # logged therein and *not* in this file.
    #
    CustomLog "logs/access_log" common

    #
    # If you prefer a logfile with access, agent, and referer information
    # (Combined Logfile Format) you can use the following directive.
    #
    #CustomLog "logs/access_log" combined
</IfModule>

[root@web conf]# vim /usr/local/apache/conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
    ...
    ErrorLog "/var/log/httpd/vhost-jikeweb-error.log"
    # 引用自定義日志格式seflcommon
    CustomLog "/var/log/httpd/vhost-jikeweb-access.log" selfcommon
</VirtualHost>

輸出格式如下:
192.168.1.1  -  -  [17/Mar/2022:21:51:41 +0800]  "GET /good.html HTTP/1.1"  200  10872  "http://192.168.1.10/"  "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36"
192.168.1.1  -  -  [17/Mar/2022:21:51:41 +0800]  "GET /js/good.js HTTP/1.1"  200  7123  "http://192.168.1.10/good.html"  "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36"

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM