Httpd服务器的日志配置

-- TOC --

Apache在运行期间,会产生两个重要的网站日志文件,分别是access.log和error.log。

这两个日志文件可以帮助我们分析网站的运行情况,但是也会带来一个问题,就是这两个日志文件会随着时间积累越来越大,各环节的处理速度也越来越慢。我们的网站都需要长年累月地提供信息服务,时间长了,这两个日志文件会非常大,甚至到几百G的量级。

网站日志文件太大,至少会带来3个问题:

  1. 影响服务器的效率,日志文件太大,再往里面写日志效率会非常低下;
  2. 磁盘空间占用过多;
  3. 日志太多,几乎已经无法分析;

但是,一般情况下,我们又不能够完全放弃日志文件。一个解决的思路是,让Apache每天生成新的日志文件。每天一个文件,管理起来会方便很多。

要达到这个目的,需要我们修改httpd.conf文件。(以下配置的rotatelogs带exe后缀,这是在windows环境下的配置)

#
# ErrorLog: The location of the error log file.
# If you do not specify an ErrorLog directive within a <VirtualHost>
# container, error messages relating to that virtual host will be
# logged here. If you *do* define an error logfile for a <VirtualHost>
# container, that host's errors will be logged there and not here.
#
#ErrorLog "logs/error.log"
ErrorLog "|bin/rotatelogs.exe -l logs/error_%Y_%m_%d.log 86400"

#
# LogLevel: Control the number of messages logged to the error_log.
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
#
LogLevel warn

<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\" %&gt;s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %&gt;s %b" common

<IfModule logio_module>
# You need to enable mod_logio.c to use %I and %O
LogFormat "%h %l %u %t \"%r\" %&gt;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 &lt;VirtualHost&gt;
# container, they will be logged here. Contrariwise, if you *do*
# define per-&lt;VirtualHost&gt; 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
CustomLog "|bin/rotatelogs.exe -l logs/access_%Y_%m_%d.log 86400" combined
</IfModule>

另外一个管理日志的思路,按VirtualHost分别保存日志

<VirtualHost *:80>
DocumentRoot "C:/maixj_sites/maixj.net"
ServerName maixj.net
ServerAlias www.maixj.net
ErrorLog "|bin/rotatelogs.exe -l ../../mysites/maixj.net/logs/error_%Y_%m_%d.log 86400"
CustomLog "|bin/rotatelogs.exe -l ../../mysites/maixj.net/logs/access_%Y_%m_%d.log 86400" combined
</VirtualHost>

<VirtualHost *:80>
DocumentRoot "C:/maixj_sites/wpchajian.cn"
ServerName wpchajian.cn
ServerAlias www.wpchajian.cn
ErrorLog "|bin/rotatelogs.exe -l ../../mysites/wpchajian.cn/logs/error_%Y_%m_%d.log 86400"
CustomLog "|bin/rotatelogs.exe -l ../../mysites/wpchajian.cn/logs/access_%Y_%m_%d.log 86400" combined
</VirtualHost>

本文链接:https://cs.pynote.net/net/httpd/202204053/

-- EOF --

-- MORE --