如何利用logrotate工具自动切分滚动中的日志文件

在很多实际项目中,应用程序会持续写日志,如果程序代码中没有调用支持自动切分(如按filesize或date切割)的日志库,则日志文件会很快增长

在很多实际项目中,应用程序会持续写日志,如果程序代码中没有调用支持自动切分(如按filesize或date切割)的日志库,则日志文件会很快增长到G级别。单机操作大文件对后续跟进日志来说非常不方便。

本文介绍如何利用logrotate这个工具来在应用程序外部切分日志。

 

1. logrotate是什么

logrotate是大多数linux系统自带的日志切割工具,在shell终端输入”man logrotate”可查看其简介(部分摘出如下):logrotate is designed to ease administration of systems that generate large numbers of log files. It allows automatic rotation, compression, removal, and mailing of log files. Each log file may be handled daily, weekly, monthly, or when it grows too large.

更完整的简介可以在shell终端查看其man文档,这里不赘述。

总之,我们知道它可以用来切割仍在滚动的日志文件即可,这里的“仍在滚动”是指当前日志文件仍在被应用程序持续进行追加写操作。

 

2. logrotate的适用场景

如果文件是静态的(即当前没有应用程序对齐进行写操作),则split是更常用的静态文件切割工具,其用法简介见这里 ,此处略过。

logrotate常用来切割仍在被写的“动态”文件,它支持 按时间间隔或文件大小 来触发文件的自动切分(automatic rotation)。具体用法下面说明。

3. 如何使用logrotate

根据man logrotate的说明,logrotate用法很简单:

logrotate [-dv] [-f|--force] [-s|--state file] config_file+

其中[]中出现的option(s)均是可选项,我们只需提供一份配置文件即可,下面用示例对配置文件格式做说明。

# sample logrotate configuration file
compress # 全局配置项,对切分后的文件做gzip压缩
# 一个配置文件中可以包含多个相互独立的sections
# 其中,待切分文件路径 + { xxx }构成了一个独立的section,每个section可以配置针对该类文件的切分行为
# 注意:全局配置项会作用于每个section,除非在该section配置中覆盖了全局配置项的行为 
# 下面section的配置表明按时间间隔(weekly)触发日志的自动切分,历史数据只保存最近的5份
# 切分完成后,通过发送-HUP信号来重启syslogd这个daemon进程。
/var/log/messages {  
  rotate 5  
  weekly
  postrotate
    /sbin/killall -HUP syslogd
  endscript
}
# 下面的示例section表明按文件大小触发日志自动切分,大小单位除了上面所示的k外,还可以是M或G
# 待切分文件路径可以有多个,多个路径用空格隔开
"/var/log/httpd/access.log" /var/log/httpd/error.log {
  rotate 5
  mail www@my.org
  size=100k
  sharedscripts # 表明HUP信号只需在所有文件均切分完成后发送一次,若无该配置,则每完成一个文件的切分就会执行一次postrotate/endscript之间配置的命令
  postrotate
    /sbin/killall -HUP httpd
  endscript
}
# 下面的示例section表明按时间(monthly)触发切分
# 切分后的历史文件会被保存到olddir指定的目录下(该目录需要事先mkdir出来,否则会报错)
/var/log/news/news.crit {
  monthly
  rotate 2
  olddir /var/log/news/old
  missingok # 若section起始处指定的待切分文件不存在,也不会报错(默认会报错)
  postrotate
    kill -HUP 'cat /var/run/inn.pid'
  endscript
  nocompress # 切分时不做压缩,这里改写了配置文件第1行的全局配置
}

上述配置文件中,”#”表示后面是注释,它可以独占一行,也可以与配置项在同一行。

配置项”rotate 5″表明切分后的历史文件最多保存最近的5份,若触发本次切分时已有5份历史文件(默认为messages.1/messages.2 /messages.3/messages.4/messages.5),则文件messages.5会被物理删除,剩余文件后缀序号依次加1。

切分的具体执行过程可以参考《鸟哥的Linux私房菜》在 这里 的说明。

上面的示例配置文件只是展示了logrotate配置文件的基本格式,该工具还支持其它众多配置项,具体可以参考man logrotate的说明。

下面的配置文件说明了在实际项目中如何按文件大小对不允许restart的应用程序进行日志自动切分。

# filename: logrotate.conf
 /home/work/running.log {
   copytruncate # 先copy running.log内容至历史文件,然后清空running.log的内容
   rotate 30	 # 保存30份历史日志 
   size=1G		# 若日志文件达到1G,则触发切分
   olddir /home/work/history # 指定历史日志存放目录
   notifempty	# 若/home/work/running.log是空文件,则不做切分
   missingok	 # 若/home/work/running.log不存在,不报错
}

特别注意 copytruncate 这个配置项,根据其文档说明(见下面的摘录), 它可能会导致部分数据丢失,在不允许日志数据丢失的应用场景下,不应该使用该配置 (若日志不允许丢失,则最好在应用程序代码中支持HUP信号来实现优雅重启,这样就可以避免使用copytruncate;当然,也可以用logrotate支持的create配置来达到目的)。

copytruncate

Truncate  the  original  log file in place after creating a copy, instead of moving the old log file and optionally creating a new one, It can be used when some program can not be told to close its logfile and thus might continue writing (appending) to the previous log file forever. Note that there is a very small time slice between copying the file and truncating it, so some logging data might be lost. When this option is used, the create option will have no effect, as the old log file stays in place.

 

【参考资料】

1. man logrotate

2. 鳥哥的Linux私房菜 – 登錄檔的輪替(logrotate)====================== EOF ===================

发表评论