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"