系統環境:ubuntu11.04+apache2
使用apache自帶的rotatelog進行日誌切割,可將日誌的文件名以年月日時分秒保存,如2011-10-21_14_20_00,設置如下:
首先在apache2的目錄下有個sites-enabled文件夾,裏面有個000-default文件,在該文件夾中加入一下內容:
CustomLog "|/usr/sbin/rotatelogs /var/log/apache2/access_log/access_log.%Y-%m_%d-%H_%M_%S 300 +480 " combined
ErrorLog "|/usr/sbin/rotatelogs /var/log/apache2/error_log/%Y%m%d 600 400M"
rotatelogs說明:
rotatelogs logfile [ rotationtime [ offset ]] | [ filesizeM ]
rotationtime指的是設定多少秒後進行日誌切割,如這裏的300秒,在設定的時間之後系統將自動切割日誌;
offset指的是日誌時間的偏移量,如果不設置該偏移量,則默認爲0,且顯示的時間與北京時間會不一樣,與北京時間相差8個小時,故建議將其設置爲+480,單位爲分鐘;
filesizeM指的是日誌多大之後自動切割,可接受的單位爲K,M,G,上面的ErrorLog設置爲400M之後自動切割日誌。
我的000-default設置如下:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
# ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
# CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>
CustomLog "|/usr/sbin/rotatelogs /var/log/apache2/access_log/access_log.%Y-%m_%d-%H_%M_%S 300 +480 " combined
ErrorLog "|/usr/sbin/rotatelogs /var/log/apache2/error_log/%Y%m%d 600 400M"
</VirtualHost>