限制Apache日誌文件大小和每天生成日誌文件的方法

access.log,件在 WEB 服務器運行一段時間之後會達到幾十兆甚至上百兆,如果Apache運行有錯誤,error.log也會增大到幾十兆,我們知道系統讀寫一個大的文本文件是非常耗內存的,因此限定日誌文件大小十分必要。

通常我們是在{$apache}/conf/httpd.conf中設置Apache的參數,然而我們並沒有發現可以設置日誌文件大小的配置指令,通過參考http://httpd.apache.org/docs/2.0/programs/rotatelogs.html,可以用apache 自己的程序 rotatelogs.exe(位於 {$apache}/bin/目錄下),來限制日誌文件的大小。

Usage: rotatelogs [-l] <logfile> <rotation time in seconds> [offset minutes from UTC] or <rotation size in megabytes>

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



Trackback: http://tb.donews.net/TrackBack.aspx?PostId=1231984

發佈了27 篇原創文章 · 獲贊 9 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章