日志切割-注意问题
日志切割-注意问题
问题一:要在linux下创建配置文件
否则会因为文件权限问题报错
error: Ignoring 文件路径 because it is writable by group or others.
问题二:因为切割日志文件目录权限问题,需要在配置文件添加
su 用户 用户组
例如:
su root root
否则会报如下错误:
error: skipping "日志文件路径" 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.
问题三:配置文件参数 size 设置不生效的问题
Logrotate配置中size参数不生效的原因分析与解决方案
可能的原因
1.在logrotate配置中,size 5M参数没有生效的主要原因是与daily
参数冲突。这两个参数是互斥的,当同时存在时,logrotate会优先使用时间条件(daily)而忽略大小条件(size 5M)。
冲突说明
互斥参数:在logrotate中,时间条件(hourly、daily、weekly、monthly、yearly)和大小条件(size)是互斥的。
优先级:当配置中同时包含这两种条件时,logrotate会优先使用时间条件,而忽略大小条件。
官方文档说明:根据logrotate的文档,当同时指定时间和大小条件时,日志会在满足任一条件时进行轮转。但实际上,许多系统的logrotate实现会优先考虑时间条件。
解决方案:可以仅使用大小条件,可以结合maxsiz或者minisize参数
2.Logrotate执行频率问题
logrotate默认通过cron每天只执行一次
即使设置了size 5M,也只有在logrotate运行时才会检查文件大小
默认情况下,logrotate通常在/etc/cron.daily/目录下配置,每天只运行一次
解决方案修改 /etc/cron.daily/logrotate 文件,将执行频率改为每小时一次
0 * * * * root /usr/sbin/logrotate /etc/logrotate.d/你的配置文件
3 设置的文件后缀问题
dateext
这个配置项会以日期时间为后缀,如果日志文件增长过快,会导致文件名已经存在从而跳过.
解决方案是去掉这个参数
在laravel中使用
提示
这个示例是结合laravel框架的日志切割
//执行调度任务
protected function schedule(Schedule $schedule)
{
//执行日志切割
$this->makeLogrotate($schedule);
}
/**
* 设置日志轮转任务
*
* 每分钟执行一次logrotate命令,轮转Laravel日志文件
* 使用Asia/Shanghai时区,在后台运行且只在单台服务器上执行
*
* @param Schedule $schedule 任务调度器实例
* @return void
*/
protected function makeLogrotate(Schedule $schedule)
{
$schedule->exec('/usr/sbin/logrotate /etc/logrotate.d/配置文件名')->cron('*/5 * * * *')->timezone('Asia/Shanghai');
}
也可以执行脚本(该脚本尚未测试成功)
# /bin/sh
# 记录执行时间
echo "Executing logrotate at $(date)" >> 项目日志路径/logrotate-execution.log
root /usr/sbin/logrotate /etc/logrotate.d/配置文件
# 记录执行结果
if [ $? -eq 0 ]; then
echo "Logrotate executed successfully" >> 项目日志路径/logrotate-execution.log
else
echo "Logrotate failed with exit code $?" >> 项目日志路径/logrotate-execution.log
fi