按日期,时间和大小旋转Docker容器上的php应用程序日志时的注释
?Monolog没有按大小旋转的功能,所以我谈到了使用Logrotate进行旋转。
配置
Docker高山php Monolog logrotate cron
Dockerfile
- 安装logrotate
1 | RUN apk add logrotate |
-
复制logrotate配置文件
事先准备一个描述日志设置的文件,然后将其复制到容器中。
在下面的示例中,感觉配置文件位于本地/日志下。
1 2 | RUN cp -rf local/log/logrotate.conf /etc/ COPY local/log/logrotate.d/php-log.conf /etc/logrotate.d |
<表格>
tr>
header>
<身体>
tr>
tr>
tr>
tbody>
table>
顺便说一下,配置文件如下所示。
本地/日志/logrotate.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | # see "man logrotate" for details # rotate log files weekly weekly # 週1ローテ # keep 4 weeks worth of backlogs rotate 4 # デフォルトのログは4世代分まで管理(週1× 4回 = 1ヶ月分) # create new (empty) log files after rotating old ones create # ローテ後に空のファイルをつくる # 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 configured here |
每天旋转一次,或者文件大小超过10M时旋转
本地/日志/logrotate.d/php-log.conf
1 2 3 4 5 6 7 8 9 10 | /var/log/php/php-app.log { daily # 1日1回ローテーション missingok rotate 30 # 30世代分 dateext # ローテ時には日付でsufix dateformat -%Y-%m-%d-%s delaycompress # ローテ後、圧縮を1回遅らせる size 10M # ファイルサイズが10Mを超えたらローテーションする su www-data www-data # 実行ユーザーを指定 } |
entrypoint.sh
重新启动cron以反映logrotate的cron设置
1 2 3 | # setup entrypoint COPY ./entrypoint.sh /usr/local/bin/ ENTRYPOINT ["entrypoint.sh"] |
entrypoint.sh看起来像这样。
入口点
1 2 3 4 5 | cp -f /etc/periodic/daily/logrotate /etc/periodic/15min crond restart chmod o+x /var/log mkdir /var/log/php chown www-data:www-data /var/log/php |
补充 h5>
logrotate旋转工作是通过cron完成的。
换句话说,每天,每周,每小时的轮换也取决于cron设置。
重要的是要知道何时使用cron发送/ etc / periodic / daily / logrotate文件。
当我查看alpine的crontab设置时,它设置为通过每小时,每小时和15分钟(可能还有更多目录)在以下目录中运行来运行,默认情况下,logrotate位于日期和时间文件夹中。已安装。
通过将logrotate执行时间设置为每15分钟一次,每天或在日志大小超过10M时执行一次旋转。
*如果logrotate执行时间每天保持一次,则即使文件大小超过10M,也仅在cron时间(即每天一次)执行旋转。
日期:/ etc /周期性/每日
每小时:/ etc /周期性/每小时
每15分钟:/ etc /定期/ 15分钟
logrotate命令
<表格>
tr>
header>
<身体>
tr>
tr>
tr>
tr>
tbody>
table>
如果发现有帮助,建议始终运行logrotate -d /etc/logrotate.conf。
如果有任何错误,例如权限或重复错误,将不会执行旋转。
如果根据php应用程序端放宽目录的权限,则权限将变得宽松,并且logrotate会生气,并且不会执行旋转。
结论
我以前做过,所以我在记住它的同时努力地写。
我想就是这样。
我没有做太多事情,但是Monolog没有能力按文件大小旋转。
即使不是Monolog或PHP,我也在写它,以为将来可能会因文件大小而轮换。