在odoo的配置文件当中,可以为odoo的日志配置存储目录
logfile=/var/log/odoo/odoo.log
但odoo忘了对日志文件定期进行分割的配置,这可能会导致日志文件过大,查询起来也不方便。幸好,Linux提供一个完美适配这一问题的命令工具——Logrotate,顾名思义,这个命令是通过轮转的方式对日志进行管理。
如果我们有接触过Linux基础命令的话,可能会了解任务计划任务管理工具cron,这个工具用于周期性执行计划任务或脚本。cron根据/etc/crontab中的配置执行任务,事实上,它会检查/etc/crontab文件及/etc/cron.d*/目录下所有的文件。
/etc/crontab
/etc/cron.daily
└── google-chrome
1 directory, 1 file
也就是说只要配置好文件,就系统就可以为我们定期执行指定的任务,这种方法能够十分方便地管理修改频率低的周期性任务。
我们需要讲到的Logrotate工具是基于cron的管理日志的工具,因此运行的逻辑与cron相似,所以想要了解logrotate的使用方法,最好从它的配置文件入手。
1. /etc/logrotate.conf
2. /etc/logrotate.d
├── bootlog
├── btmp
├── chrony
├── dnf
├── firewalld
├── glusterfs
├── httpd
...
2 directories, 20 files
我们需要熟悉的是/etc/logrotate.conf配置文件以及logrotate.d目录,logrotate加载logrotate.conf中的配置,根据配置文件中配置的任务执行任务,而在logrotate目录下管理的是不同服务的日志配置文件,有我们熟悉的httpd、mysql、redis等等,这些文件会由logrotate.conf配置文件加载并执行。
所以首先,我们来看logrotate.conf内部的代码是什么
# see "man logrotate" for details
# global options do not affect preceding include directives
# rotate log files weekly
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
# packages drop log rotation information into this directory
include /etc/logrotate.d
# system-specific logs may also be configured here.
在logrotate中,除了配置了默认的一些配置外,最主要还是在最后的include /etc/logrotate.d,这一条命令导入了logrotate.d中的所有配置文件,所以logrotate先是根据logrotate.d目录下的配置执行命令,对于缺省的配置信息再由logrotate.conf中的默认配置进行补充。
最后我们只需要了解都有那些配置信息就可以基本了解logrotate命令了,参考redis,其配置信息大致如下:
/var/log/redis/*.log {
weekly
rotate 10
copytruncate
delaycompress
compress
notifempty
missingok
}
/var/log/redis/*.log为选择/var/log/redis/目录下所有后缀为log的日志文件;
weekly为名每周执行一次;
rotate 10为保存10个轮次的日志文件;
copytruncate为用于打开的日志文件,先进行备份再进行截断;
delaycopress与compress一同使用,为直到下一轮次在对备份的日志文件进行压缩;
compress通过gzip压缩转存后的日志文件;
notifempty为当日志文件为空时不进行轮转;
missingok为日志缺失之后不报错。
在logrotate.d目录下的配置文件的格式大致都是这样,当我们有什么日志文件需要备份或切割时,可以按照这样的格式进行配置。
这里对配置项作一些补充:
compress 通过gzip 压缩转储以后的日志 nocompress 不做gzip压缩处理 copytruncate 用于还在打开中的日志文件,把当前日志备份并截断;是先拷贝再清空的方式,拷贝和清空之间有一个时间差,可能会丢失部分日志数据。 nocopytruncate 备份日志文件不过不截断 create mode owner group 轮转时指定创建新文件的属性,如create 0777 nobody nobody nocreate 不建立新的日志文件 delaycompress 和compress 一起使用时,转储的日志文件到下一次转储时才压缩 nodelaycompress 覆盖 delaycompress 选项,转储同时压缩。 missingok 如果日志丢失,不报错继续滚动下一个日志 errors address 专储时的错误信息发送到指定的Email 地址 ifempty 即使日志文件为空文件也做轮转,这个是logrotate的缺省选项。 notifempty 当日志文件为空时,不进行轮转 mail address 把转储的日志文件发送到指定的E-mail 地址 nomail 转储时不发送日志文件 olddir directory 转储后的日志文件放入指定的目录,必须和当前日志文件在同一个文件系统 noolddir 转储后的日志文件和当前日志文件放在同一个目录下 sharedscripts 运行postrotate脚本,作用是在所有日志都轮转后统一执行一次脚本。如果没有配置这个,那么每个日志轮转后都会执行一次脚本 prerotate 在logrotate转储之前需要执行的指令,例如修改文件的属性等动作;必须独立成行 postrotate 在logrotate转储之后需要执行的指令,例如重新启动 (kill -HUP) 某个服务!必须独立成行 daily 指定转储周期为每天 weekly 指定转储周期为每周 monthly 指定转储周期为每月 rotate count 指定日志文件删除之前转储的次数,0 指没有备份,5 指保留5 个备份 dateext 使用当期日期作为命名格式 dateformat .%s 配合dateext使用,紧跟在下一行出现,定义文件切割后的文件名,必须配合dateext使用,只支持 %Y %m %d %s 这四个参数 size(或minsize) log-size 当日志文件到达指定的大小时才转储,log-size能指定bytes(缺省)及KB (sizek)或MB(sizem). 当日志文件 >= log-size 的时候就转储。 以下为合法格式:(其他格式的单位大小写没有试过) size = 5 或 size 5 (>= 5 个字节就转储) size = 100k 或 size 100k size = 100M 或 size 100M
参考博文:
https://blog.csdn.net/DLXCW/article/details/138037606
https://www.cnblogs.com/txlsz/p/13126723.html
https://zhuanlan.zhihu.com/p/485867081
logrotate 日志文件管理工具