我有一个运行在Docker上的Ruby on Rails应用程序,我想每天轮换我的生产日志。本着保持一切独立的精神,我也想在Docker本身上保持日志旋转。以下是Docker容器上的logrotate配置:
/etc/logrotate.conf
# rotate log files weekly
weekly
# keep 1 week worth of backlogs
rotate 1
# 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
# packages drop log rotation information into this directory
include /etc/logrotate.d
# system-specific logs may be also be configured here.
# Rotate Rails logs
/myapp/log/*.log {
daily
missingok
rotate 7
compress
delaycompress
notifempty
copytruncate
}
权限/etc/logrotate.conf:
-rw-r--r-- 1 root root 656 Jul 13 02:06 /etc/logrotate.conf
一天之后,我的日志文件没有被旋转。我知道这不是我的容器被重新创建的问题,因为它已经运行了6天。
当我去测试它时,每行都有一个错误消息:
> logrotate -d /myapp/log/production.log
...
> error: production.log:56257 unknown option 'I' -- ignoring line
> error: production.log:56258 unknown option 'I' -- ignoring line
> error: production.log:56259 unknown option 'I' -- ignoring line
以下是我的production.log的权限:
-rw-r--r-- 1 root root 10868754 Jul 14 12:42 /myapp/log/production.log
当我检查日志文件中的内容时,内容确实存在:
I, [2022-07-13T02:48:23.666904 #1] INFO -- : Raven 3.1.0 ready to catch errors
I, [2022-07-13T03:00:18.483790 #8] INFO -- : Raven 3.1.0 ready to catch errors
I, [2022-07-13T03:00:34.021416 #22] INFO -- : Started GET "/healthcheck" for xxx.xx.xx.xx at 2022-07-13 03:00:34 +0000
I, [2022-07-13T03:00:34.026047 #22] INFO -- : Processing by HealthcheckController#show as HTML
I, [2022-07-13T03:00:34.027170 #29] INFO -- : Started GET "/healthcheck" for xxx.xx.xx.xx at 2022-07-13 03:00:34 +0000
I, [2022-07-13T03:00:34.031331 #29] INFO -- : Processing by HealthcheckController#show as HTML
I, [2022-07-13T03:00:34.038132 #20] INFO -- : Started GET "/healthcheck" for xxx.xx.xx.xx at 2022-07-13 03:00:34 +0000
I, [2022-07-13T03:00:34.042771 #20] INFO -- : Processing by HealthcheckController#show as HTML
I, [2022-07-13T03:00:34.040177 #20] INFO -- : Started GET "/healthcheck" for xxx.xx.xx.xx at 2022-07-13 03:00:34 +0000
I, [2022-07-13T03:00:34.221546 #20] INFO -- : Processing by HealthcheckController#show as HTML
我怎么做才能让logrotate
旋转我的日志?
感谢@β.εηοιτ。在调试它时,我发现我需要在logrotate参数中指定配置文件,而不是日志文件,如下所示:
logrotate -d /etc/logrotate.conf
这表明我的配置没有任何问题。然后我使用:
强制日志旋转logrotate -f /etc/logrotate.conf
它成功了!至于为什么我的日志文件没有在一天后旋转,这篇文章建议cron可能没有在容器中运行,我发现在我的情况下是真的:
> service cron status
[FAIL] cron is not running ... failed!
我更新了我的Dockerfile的入口点来启动cron
。现在看起来是这样的:
Dockerfile
...
CMD /bin/bash
ENTRYPOINT [ "./docker/web-entrypoint.sh" ]
码头工人/web-entrypoint.sh
#!/bin/sh
set -e
if [ -f tmp/pids/server.pid ]; then
rm tmp/pids/server.pid
fi
cron service start # New line added
bundle exec rails s -b 0.0.0.0 -p 3000
现在一切正常了!