10 log のローテート とりあえずまとめ

昔は、自分でスクリプトを書いて、cron で回していたようだが、 今は newsyslog, logrotate などにお任せするものか。

1. newsyslog

macOS に用意されていて、 /etc/newsyslog.conf に色々設定を書いて利用する。

newsyslog については、FreeBSD のマニュアル https://man.freebsd.org/newsyslog.confが詳しい。

Mac の /etc/newsyslog.conf は、元々は
# logfilename          [owner:group]    mode count size when  flags [/pid_file] [sig_num]
/var/log/ftp.log			640  5	   1000	*     J
/var/log/hwmond.log			640  5	   1000	*     J
/var/log/ipfw.log			640  5	   1000	*     J
/var/log/lpr.log			640  5	   1000	*     J
/var/log/ppp.log			640  5	   1000	*     J
/var/log/wtmp				644  3	   *	@01T05 B
のような内容。

例えば次のようなことを書き足す。
/var/log/sudo.log root:wheel 600  365  *  @T00  J
/var/log/sshd-log root:wheel 600  365  1000  @T00  J

新規に追加するものは /etc/newsyslog.d/ におく、という手もある。

それを有効にするには、/etc/newsyslog.conf の末尾に
<include> /etc/newsyslog.d/*
のように書く。なるほど。

2. /var/log/system.log

/var/log/system.log は基本的なログファイルである。 私は当初 newsyslog かと思ったけれど、 実はこれは /etc/asl.conf で指定されている。
% grep system.log /etc/asl.conf
# Rules for /var/log/system.log
> system.log mode=0640 format=bsd rotate=seq compress file_max=5M all_max=50M
? [= Sender kernel] file system.log
? [<= Level notice] file system.log
? [= Facility auth] [<= Level info] file system.log
? [= Facility authpriv] [<= Level info] file system.log

どうも世代数はデフォールトで7のようである。 30世代にするには
> system.log mode=0640 format=bsd rotate=seq compress file_max=5M all_max=50M ttl=30
とする (そうしてみて様子を見る)。

3. logrotate

その名前もズバリ、logrotate というソフトウェアがある。

Linux のマニュアル https://linux.die.net/man/8/logrotate

MacPorts でインストールできる。
sudo port install logrotate

/Library/LaunchDaemons/org.macports.logrotate.plist が用意される。 (/opt/local/etc/LaunchDaemons/org.macports.logrotate/org.macports.logrotate.plist へのシンボリック・リンクである。)

次のコマンドで有効となる。
sudo port load logrotate

基本的な設定ファイル
/opt/local/etc/logrotate.conf
# see "man logrotate" for details
# Rotate log files weekly.
weekly

# Keep 52 weeks worth of backlogs.
rotate 52

# Create new (empty) log files after rotating old ones.
create

# Use date as a suffix of the rotated file.
dateext

# Compress log files.
compress

# Add your logrotate scripts to this directory for convenient inclusion.
include /opt/local/etc/logrotate.d

個々の設定は /opt/local/etc/logrotate.d/名前 で指定する。

例として
/opt/local/etc/logrotate.d/httpd
/opt/local/var/log/apache2/*log {
    weekly
    rotate 52
    compress
    delaycompress
    missingok
    notifempty
    create 0640 root admin
    sharedscripts
    postrotate
        /opt/local/sbin/apachectl -k graceful > /dev/null 2>&1 || true
    endscript
}

postrotate があるので柔軟である。



桂田 祐史