oro*_*aki 3 nginx log-files logging web-server apache-2.2
注意:我使用的是 Apache2(在 Linux 上),但我问的是一般意义上的(仅适用于 Linux),因为我也想知道一般意义上的“最佳”方法(因为我即将在 Nginx 或 Cherokee 上部署一个大型站点)。
几周后,我的日志文件变得很大。我需要暂时保留它们,但我想删除超过 2 周左右的条目。这可能吗,我该怎么做?
你可以使用 logrotate。它根据特定服务的配置文件轮换日志。它通常由 cron 每天运行。
apache 的配置文件示例 /etc/logrotate.d/apache2
/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
}
Run Code Online (Sandbox Code Playgroud)
这个会:
如果您不想重新加载进程,那么您应该使用copytruncate,它将当前内容复制到一个新文件中,压缩它,然后截断当前日志文件。在这种情况下,您不需要sharedscripts,postrotate和endscript。