Apache 日志文件轮换

Apache 日志文件轮换

我有包含文件的 /etc/logrotate.d 目录;每个服务(例如 httpd)都有自己的 logrotate 配置文件。

/etc/logrotate.d 目录中的内容配置为 5k 之后轮换。

我还有一个 /etc/logrotate.conf 文件,它还没有包含在 cron 中。它被配置为:每周轮换日志文件

weekly   # keep 4 weeks worth of backlogs
rotate 4 # create new (empty) log files after rotating old ones
create   # use date as a suffix of the rotated file
dateext  # uncomment this if you want your log files compressed
compress 

# RPM packages drop log rotation information into this directory
include /etc/logrotate.d

# no packages own wtmp and btmp -- we'll rotate them here
/var/log/wtmp { monthly create 0664 root utmp minsize 1M rotate 1 }
/var/log/btmp { monthly create 0664 root utmp minsize 1M rotate 1 }

以下是 /etc/logrotate.d/ 目录中的典型配置文件:

/var/log/httpd/*log { size 5k 
missingok 
notifempty 
sharedscripts 
compress 
sharedscripts 
postrotate /sbin/service httpd reload > /dev/null 2>/dev/null || true endscript }

我的问题是,我还必须在服务器上的哪个位置进行配置,以便 /etc/logrotate.d 目录中的文件在 5k 之后轮换。并且 /etc/logrotate.conf 是否配置为按配置轮换?

答案1

在我看来,这一切都很好。你根本不需要改变任何东西。一旦 logrotate 开始执行,事情就会开始按预期进行轮换。一个警告是,可能需要一个完整的轮换周期才能真正开始获得旋转。有内置的安全措施可以防止旋转文件经常。您可以通过查看 logrotate 状态文件来监控该过程:/var/lib/logrotate.status。它包含 logrotate 正在管理的所有文件的列表以及每个文件的最后轮换时间。

答案2

听起来不错,但请注意,logrotate 不是守护进程,日志不会以 5k 为单位轮换,只有当执行 logrotate 时大小大于 5k 时才会轮换。默认情况下,它每天只运行一次。

man logrotate

通常情况下,logrotate 作为每日 cron 作业运行。

logrotate cron 文件通常是。如果您希望 logrotate 每小时验证一次日志,或者创建自定义 cron 以更频繁地执行它,则/etc/cron.daily/logrotate必须将其移入。/etc/cron.hourly

答案3

logrotate -d config_file
   -d     Turns on debug mode and implies -v.  In debug mode, no changes will be made to the logs or to the logrotate state file.

相关内容