Apache下的access.log和error.log文件处理方法
这几天有会员和我说到网站访问速度越来越慢。我查了一下,看到Apache2下面有两个日志文件非常大了,加起来有800多M了。
分别是access.log和error.log。
于是在网上找找了给access.log和error.log减肥的方法,这个方法可让这两个文件按每天日期生成。这样你可以选择
把前面的旧文件删除了。
在Apache下的httpd.conf配置文件下找到下面两句:
ErrorLog logs/error.log
CustomLog logs/access.log common
然后把这两句分别改为如下:
CustomLog “|D:/apache2/bin/rotatelogs.exe D:/apache2/logs/access_%Y_%m_%d.log 86400 480″ common
ErrorLog “|D:/apache2/bin/rotatelogs.exe D:/apache2/logs/error_%Y_%m_%d.log 86400 480″
一切就这么简单,这样这两个日志文件每天都会起一个新文件,就不至于单个文件太大,打不开,而无法看到日志信息了.
你还可以把前面的日志文件删除。
access.log,件在 WEB 服务器运行一段时间之后会达到几十兆甚至上百兆,如果Apache运行有错误,error.log也会增大到几十兆,我们知道系统读写一个大的文本文件是非常耗内存的,因此限定日志文件大小十分必要。
日志文件大小的配置指令,通过参考http://httpd.apache.org/docs/2.0/programs /rotatelogs.html,可以用apache 自己的程序 rotatelogs.exe(位于 {$apache}/bin/目录下),来限制日志文件的大小。
Usage: rotatelogs [-l] [offset minutes from UTC] or
Add this:
TransferLog “|rotatelogs /some/where 86400″
or
TransferLog “|rotatelogs /some/where 5M”
to httpd.conf. The generated name will be /some/where.nnnn where nnnn is the system time at which the log nominally starts (N.B. if using a rotation time, the time will always be a multiple of the rotation time, so you can synchronizecron scripts with it). At the end of each rotation time or when the file size is reached a new log is started.
在 Windows 下的设置例子如下:
# 限制错误日志文件为 1M
ErrorLog “|bin/rotatelogs.exe -l logs/error-%Y-%m-%d.log 1M”
# 每天生成一个错误日志文件
#ErrorLog “|bin/rotatelogs.exe -l logs/error-%Y-%m-%d.log 86400″
# 限制访问日志文件为 1M
CustomLog “|bin/rotatelogs.exe -l logs/access-%Y-%m-%d.log 1M” common
# 每天生成一个访问日志文件
#CustomLog “|bin/rotatelogs.exe -l logs/access-%Y-%m-%d.log 86400″ common
linux/Unix下 应该类似。