9.2. 日志转储
linux的日志转储有logrorate提供,可以安装时间,文件大小来自动滚动日志文件,简化日志管理。
logrotate程序提供用于压缩日志文件,发送日志信息到指定的email功能。
9.2.1. logrorate的主要配置参数
参数:
参数 |
描述 |
|---|---|
compress |
启用压缩,默认gzip的 |
compresscmd |
压缩命令,默认是gzip,可以改 |
uncompresscmd |
解压命令,默认是gunzip |
compressext |
压缩扩展 |
compressoptions |
压缩选项,比如-9指定压缩级别 |
copy |
复制日志文件,但不要更改原始文件。 |
copytruncate |
在创建副本后,将原始日志文件截断,而不是移动旧日志文件,并选择创建新日志文件 |
create mode owner group |
创建新的时候来控制创建文件的权限和所属信息 |
daily |
每日去滚动 |
dateext |
使用YYYYMMDD这种后缀,而不是简单的序号,格式收dateformat影响 |
dateformat format_string |
默认值是-%Y%m%d,具体格式参考date命令格式即可 |
delaycompress |
延迟压缩,将前一个日志文件的压缩延迟到下一个旋转周期 |
extension ext |
默认是.gz的 |
ifempty |
即使是空的,也要滚动 |
include file_or_directory |
从指定的文件来读取配置信息 |
mail address |
当一个日志不存在时,它就被发送到地址 |
mailfirst |
在使用邮件命令时,只需发送刚刚旋转的文件,而不是即将到期的文件。 |
maillast |
当使用邮件命令时,要发送即将到期的文件,而不是仅仅旋转的文件(这是默认的)。 |
maxage count |
如果count配置3,就是超过3天的可以删掉了。 |
minsize size |
单位bytes,日志文件在大于size字节时旋转 |
missingok |
如果缺失,没有错误信息 |
monthly |
每月滚动 |
olddir |
日志被移动到目录中进行旋转 |
postrotate/endscript |
滚动后脚本 |
prerotate/endscript |
滚动前脚本 |
firstaction/endscript |
|
lastaction/endscript |
|
rotate count |
保留个数,如果0,就只有一个当前日志了 |
size size |
只有超过这个大小才滚动 |
sharedscripts |
|
shred |
使用shred删除文件 |
shredcycles count |
删除之前重新文件的次数 |
start count |
开始编号 |
weekly |
周滚动 |
yearly |
年滚动 |
nocompress |
- |
nocopy |
- |
nocopytruncate |
- |
nocreate |
- |
nodelaycompress |
- |
nodateext |
- |
nomail |
- |
nomissingok |
- |
noolddir |
- |
nosharedscripts |
- |
noshred |
- |
notifempty |
- |
9.2.2. 样例的配置文件
全局的配置文件
1# see "man logrotate" for details
2# rotate log files weekly
3weekly
4
5# keep 4 weeks worth of backlogs
6rotate 4
7
8# create new (empty) log files after rotating old ones
9create
10
11# use date as a suffix of the rotated file
12dateext
13
14# uncomment this if you want your log files compressed
15#compress
16
17# RPM packages drop log rotation information into this directory
18include /etc/logrotate.d
19
20# no packages own wtmp and btmp -- we'll rotate them here
21/var/log/wtmp {
22 monthly
23 create 0664 root utmp
24 minsize 1M
25 rotate 1
26}
27
28/var/log/btmp {
29 missingok
30 monthly
31 create 0600 root utmp
32 rotate 1
33}
34
35# system-specific logs may be also be configured here.
9.2.2.1. httpd
1[root@localhost logrotate.d]# cat httpd
2/var/log/httpd/*log {
3 missingok
4 notifempty
5 sharedscripts
6 delaycompress
7 postrotate
8 /bin/systemctl reload httpd.service > /dev/null 2>/dev/null || true
9 endscript
10}
9.2.2.2. bootlog
1[root@localhost logrotate.d]# cat bootlog
2/var/log/boot.log
3{
4 missingok
5 daily
6 copytruncate
7 rotate 7
8}
9.2.2.3. syslog
1[root@localhost logrotate.d]# cat syslog
2/var/log/cron
3/var/log/maillog
4/var/log/messages
5/var/log/secure
6/var/log/spooler
7{
8 missingok
9 sharedscripts
10 postrotate
11 /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
12 endscript
13}
9.2.2.4. yum
1[root@localhost logrotate.d]# cat yum
2/var/log/yum.log {
3 missingok
4 notifempty
5 size 30k
6 yearly
7 create 0600 root root
8}