Nginx 日志按天切割


(Frederic Chan) #1

Shell 脚本如下:

# 日志保存位置
base_path='/home/wwwroot/NG1.8-MQ5.5-P7/logs'

# 获取当前年信息和月信息
log_path=$(date -d yesterday +"%Y%m")

# 获取昨天的日信息
day=$(date -d yesterday +"%d")

# 按年月创建文件夹
mkdir -p $base_path/$log_path

for file in `find ./ -name "*.log"`
do
	#echo $file
	#echo ${file##*/}
	
	# 备份昨天的日志到当月的文件夹
	mv $file $base_path/$log_path/${file##*/}_$day.log
done

# 输出备份日志文件名
# echo $base_path/$log_path/access_$day.log


# 通过Nginx信号量控制重读日志
kill -USR1 `cat /usr/local/nginx-1.12/logs/nginx.pid`

添加可执行权限:

chmod +x ./log_rotate.sh

添加计划任务:

crontab -e

新增一行(每天0时1分进行日志分割(建议在02-04点之间,系统负载小)):

01 00 * * * /home/wwwroot/NG1.8-MQ5.5-P7/logs/log_rotate.sh

crontab 相关命令:

service crond start
service crond stop
service crond restart
service crond reload

#查看当前用户的crontab
crontab -l

#删除crontab
crontab -r

(Frederic Chan) #2

其实作为程序员还是应该避免自己重复造轮子,其实 Linux 自带了一个工具叫做 logrotate 已经可以很好的实现日志轮转了。

supervisor 的适配方法

One feature supervisord provides is log handling. By default supervisord 3.0 will keep your stdout and stderr logs trimmed down to 50Mb before it rotates it to a log.1 file and keeps up to 10 backup logs. This can get you up and running with no fuss. Great!

Then came the time where we wanted more control over our logs. We needed to keep a really long backtrace on the machines and gzip the backup files, so that they don’t take too much disk space. Supervisord doesn’t give you much control over log rotation, but logrotate does.

Logrotate is a standard UNIX application that allows automatic rotation, compression, removal, and mailing of log files. We wanted to share our simple supervisord + logrotate setup that allows you to disable supervisord’s log rotation feature and replace it with logrotate’s:

[program:my_app]
directory=/opt/%(program_name)s
command=/opt/%(program_name)s/run

stderr_logfile=/var/log/supervisor/%(program_name)s_stderr.log
stdout_logfile=/var/log/supervisor/%(program_name)s_stdout.log

stdout_logfile_maxbytes=0
stderr_logfile_maxbytes=0

stdout_logfile_backups=0
stderr_logfile_backups=0

%(program_name)s is one of the template arguments supervisord passes into the file and is evaluated against a python dictionary.
The stderr_logfile and stdout_logfile set a specific file name instead of supervisord’s auto generated one. Setting x_maxbyte=0 disables limiting the log size, and x_backups=0 disables rotation backups.

Now we setup logrotate with the following configuration:

/var/log/supervisor/my_app_*.log {
daily
rotate 60
copytruncate
compress
missingok
notifempty
}

This config will rotate our log daily and keep 60 compressed backups without limiting the file size.
Logrotate is run by crond. We recommend having a default setup for logrotate that runs daily.
Make sure that if you want to rotate more than once a day, you will also need to setup logrotate to run more than once a day in your crontab.
Enjoy your new logging setup!

我的个人配置如下:

/var/log/supervisor/*.log {
 daily
 rotate 60
 size 5M
 copytruncate
 nocompress
 dateext
 missingok
 notifempty
}

修改 daily crontab 执行时间

Logrotate是基于CRON来运行的,其脚本是 /etc/cron.daily/logrotate,实际运行时,Logrotate会调用配置文件/etc/logrotate.conf。由于每日的 crontab 默认在三点左右执行,这与我们按天划分日志的需求不一致,所以需要修改配置。

crontab 的配置文件地址为/etc/anacrontab(老版本的文件是/etc/crontab),里面注释写的很清楚,这里不再赘述了。


参考:

  1. Easy logging with logrotate and supervisord
  2. 运维中的日志切割操作梳理(Logrotate/python/shell脚本实现)