周期性归档 laravel 日志

文章目录

    周期性归档 laravel 日志

    laravel 内置的日志功能,没有归档功能(按日期归档那个太简单了,不能限制个数)。

    而 Ubuntu 内置的 logrotate 功能正好可以用来处理 laravel 日志。

    参考:Log Rotation with logrotate - 运维

    在 /etc/logrotate.d 目录下,新建一个 laravel 文件,内容拷贝 nginx 的配置即可,删掉没用的部分。

    su www-data www-data
    /xxxstorage/logs/*.log {
            daily
            missingok
            rotate 14
            compress
            delaycompress
            notifempty
            create 0775 www-data www-data
    }
    

    测试

    /usr/sbin/logrotate -f /etc/logrotate.d/laravel 
    
    error: failed to rename /xxx/storage/logs/laravel.log to /xxx/storage/logs/laravel.log.1: Permission denied
    error: error creating output file /var/lib/logrotate/status.tmp: Permission denied
    
    sudo /usr/sbin/logrotate -f /etc/logrotate.d/laravel 
    
    error: skipping "/xxx/storage/logs/laravel.log" because parent directory has insecure permissions (It's world writable or writable by group which is not "root") Set "su" directive in config file to tell logrotate which user/group should be used for rotation.
    

    在配置文件首行中添加上

    su www-data www-data
    

    之后再次执行,就运行正常了。

    参考

    https://www.authenticdesign.co.uk/using-logrotate-laravel-projects/

    关于作者 🌱

    我是来自山东烟台的一名开发者,有感兴趣的话题,或者软件开发需求,欢迎加微信 zhongwei 聊聊,或者关注我的个人公众号“大象工具”, 查看更多联系方式