实战:logrotate 日志轮转及配置
1,编写系统日志的配置文件 /etc/rsyslog.conf
2,写入配置
[root@A logrotate.d]# cat /etc/rsyslog.conf
# rsyslog v5 configuration file
*.* /var/log/admin.log(所有的服务,所有级别的日志全部写入到此路径下面)
3,重启rsyslog服务,检查并且查看是否有日志生成
[root@A logrotate.d]# ll /var/log/admin.log
-rw-------. 1 root root 0 Sep 4 01:38 /var/log/admin.log
[root@A logrotate.d]# /etc/init.d/rsyslog restart
4,查看日志轮转配置文件。以及相关的格式
[root@A logrotate.d]# cat /etc/logrotate.conf
# 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 (调用子配置文件,通常把日志的策略放在此路径下,方便管理)
5,在/etc/logrorate.d 路径下为系统日志/var/log/admin.log 创建策略
vim /etc/logrorate.d/admin
[root@A logrotate.d]# cat admin
/var/log/admin.log {
daily 每天轮转
rotate 3 保留3个备份
minsize 100k 大小至少100k才开始轮转
missingok 丢失不提示
compress 压缩文件
create 0600 root root 建新文件并设权
dateext 日志文件以日期命名
}
6,可以强制检测,轮转策略是否生效
logrotate -f /etc/logrorate.d/admin
[root@A logrotate.d]# ll /var/log/admin*
-rw-------. 1 root root 0 Sep 4 01:38 /var/log/admin.log
-rw-------. 1 root root 469 Sep 4 01:38 /var/log/admin.log.1
-rw-------. 1 root root 187 Sep 4 01:38 /var/log/admin.log-20170904.gz