11.2. 日志转储

linux的日志转储有logrorate提供,可以安装时间,文件大小来自动滚动日志文件,简化日志管理。

logrotate程序提供用于压缩日志文件,发送日志信息到指定的email功能。

11.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

-

11.2.2. 样例的配置文件

全局的配置文件

# 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
}

# system-specific logs may be also be configured here.

httpd

[root@localhost logrotate.d]# cat httpd
/var/log/httpd/*log {
    missingok
    notifempty
    sharedscripts
    delaycompress
    postrotate
        /bin/systemctl reload httpd.service > /dev/null 2>/dev/null || true
    endscript
}

bootlog

[root@localhost logrotate.d]# cat bootlog
/var/log/boot.log
{
    missingok
    daily
    copytruncate
    rotate 7
}

syslog

[root@localhost logrotate.d]# cat syslog
/var/log/cron
/var/log/maillog
/var/log/messages
/var/log/secure
/var/log/spooler
{
    missingok
    sharedscripts
    postrotate
    /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
    endscript
}

yum

[root@localhost logrotate.d]# cat yum
/var/log/yum.log {
    missingok
    notifempty
    size 30k
    yearly
    create 0600 root root
}