logrotate切割pm2日志

pm2的日志文件默认在根目录下的.pm2/logs文件夹中,每个程序都会自动生成一个out.log和error.log文件,但是程序的日志只会写在这个两个文件中,并不会自动分割。那么,蛋疼的问题就来了,假如你在定位问题,怎么知道当前的请求会被记录哪个log里面呢?或者说你想查看今天一天的记录会被写到哪个log里面呢?此时,你就会很苦恼的骂pm2!

有经验的同事会告诉你,你可以安装一个pm2库叫pm2-logrotate。打开github地址,可以看到它的使用方法,很简单安装即可使用!but下面我说的是利用系统的logrotate工具来实现切割pm2的日志。

什么是logrotate?

Linux logrotate命令用于管理记录文件。

使用logrotate指令,可让你轻松管理系统所产生的记录文件。它提供自动替换,压缩,删除和邮寄记录文件,每个记录文件都可被设置成每日,每周或每月处理,也能在文件太大时立即处理。您必须自行编辑,指定配置文件,预设的配置文件存放在/etc目录下,文件名称为logrotate.conf。

配置文件

logrotate的配置文件是 /etc/logrotate.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  # see "man logrotate" for details
# 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

# RPM packages drop log rotation information into this directory
include /etc/logrotate.d

# no packages own wtmp and btmp -- we'll rotate them here
/var/log/wtmp {
monthly
create 0664 root utmp
minsize 1M
rotate 1
}

/var/log/btmp {
missingok
monthly
create 0600 root utmp
rotate 1
}

这个文件是logroate的默认配置文件,即缺省配置!

  • weekly是每周执行一次;
  • rotate 4表示保留几个切割后的版本;
  • create 切割完成后创建一个新的log文件;
  • dateext 是切割后会对文件重命名添加一个日期格式的后缀,如xxxx.log_20171229;
  • include 是一个配置目录,它是各软件使用logrotate分割日志文件所使用的配置文件;
  • 最后那个是两个rotate示例。

如何定义一个logrotate配置?

在上面的配置文件中有了两个任务的配置,我们需要做的是在/etc/logrotate.d新建一个配置文件

以pm2为例,首先我们在logroate的配置目录下新建一个pm2文件,内容如下:

1
2
3
4
5
6
7
8
9
10
11
# 将下面内容写入pm2文件中
/export/webadmin/.pm2/logs/* {
daily
rotate 12
dateext
missingok
notifempty
compress
delaycompress
copytruncate
}

/export/webadmin/.pm2/logs/ 这里对应你需要切割的日志目录!保存就ok了!

看下各参数的解释

  • daily: 日志文件将按日轮循。其可用值为‘daily’,‘weekly’或者‘yearly’。
  • rotate 12: 一次将存储12个归档日志。对于第13个归档,时间最久的归档将被删除。
  • dateext: 日志文件以创建日期命名。
  • missingok: 在日志轮转期间,任何错误将被忽略,例如“文件无法找到”之类的错误。
  • notifempty: 如果日志文件为空,轮转不会进行。
  • compress: 在轮转任务完成后,已轮循的归档将使用gzip进行压缩。
  • delaycompress: 总是与compress选项一起用,delaycompress选项指示logrotate不要将最新的轮转文件压缩,而等到下一次轮转的时候再执行压缩!这样最新的轮转文件是一个可读的而不是压缩包文件。
  • copytruncate: 用于还在打开中的日志文件,把当前日志备份并截断。
  • nocopytruncate 备份日志文件但是不截断。
  • mail: 轮转的日志文件发送到指定的E-mail 地址。
  • prerotate/endscript: 轮转以前需要执行的命令,这两个关键字必须单独成行。
  • postrotate/endscript: 轮转以后需要执行的命令,这两个关键字必须单独成行。

执行与调试

logrotate可以在任何时候从命令行手动调用。

1
2
3
4
5
6
7
8
9
  -d, --debug               Don't do anything, just test (implies -v)
-f, --force Force file rotation
-m, --mail=command Command to send mail (instead of `/bin/mail')
-s, --state=statefile Path of state file
-v, --verbose Display messages during rotation

Help options:
-?, --help Show this help message
--usage Display brief usage message

测试logrotate

1
logrotate -d /etc/logrotate.d/pm2

d 是debug即验证我们写的配置文件,不会实际轮转任何日志文件,可以模拟日志轮转并显示输出。

强制轮转

即使轮循条件没有满足,我们也可以通过使用‘-f’选项来强制logrotate轮循日志文件,‘-v’参数提供了详细的输出。

1
logrotate -vf /etc/logrotate.d/pm2

轮转日志&排障

logrotate自身的日志都存放在 /var/lib/logrotate.status 文件中。

如果出于排障目的,我们想要logrotate记录到任何指定的文件,我们可以指定像下面这样从命令行指定。

1
logrotate -vf –s /var/log/pm2-logrotate.log /etc/logrotate.d/pm2

logrotate的执行时间

logrotate是基于cron运行的,在centos 6以上的版本中,是基于 anacrontab 实现的,配置文件在 /etc/anacrontab 。在centos 5及以下版本中,是基于crontab实现的,配置文件 /etc/crontab 。

anacrontab 配置文件如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# /etc/anacrontab: configuration file for anacron

# See anacron(8) and anacrontab(5) for details.

SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22

#period in days delay in minutes job-identifier command
1 5 cron.daily nice run-parts /etc/cron.daily
7 25 cron.weekly nice run-parts /etc/cron.weekly
@monthly 45 cron.monthly nice run-parts /etc/cron.monthly

START_HOURS_RANGE定义了logrotate执行的时间,上面就是3点22执行!

请注意 anacron 会在启动机器之后,频频执行此类活动,有可能导致计算机反应迟缓。这也正是/etc/anacrontab 文件中的任务以 nice 命令开头的原因,这样可以减小它们执行优先级进而限制它们对系统的影响。