注意:我正在使用Apache2(在Linux上),但是我问了一般意义上的问题(仅适用于Linux),因为我也想知道在一般情况下完成此操作的“最佳”方式(因为我即将在Nginx或Cherokee上部署一个大型站点)。
短短几周后,我的日志文件就变得很大了。 我需要暂时保留它们,但是我想删除大于2周的条目。 这是可能的,我该怎么做?
你可以使用logrotate。 它根据特定服务的configuration文件旋转日志。 它通常由cron每天运行。
/etc/logrotate.d/apache2 apacheconfiguration文件的一个例子
/var/log/apache2/*_log { daily rotate 31 missingok compress delaycompress sharedscripts postrotate if [ -f "`. /etc/apache2/envvars ; echo ${APACHE_PID_FILE:-/var/run/apache2.pid}`" ]; then /etc/init.d/apache2 reload > /dev/null fi endscript }
这个会:
如果你不想重载进程,那么你应该使用copytruncate ,它将当前的内容复制到一个新的文件,压缩它,然后截断当前的日志文件。 在这种情况下,你不需要sharedscripts , endscript和endscript 。
处理日志的规范工具是logrotate 。