apache2.4.7に付いてきたrotatelogsがリンクとプログラム経由を提供していたので
日付でrotateしつつgzipしてdeleteまでしてみた。
公式はこちら
http://httpd.apache.org/docs/2.4/programs/rotatelogs.html
apache側のカスタムログ指定
http.conf
CustomLog "|/home/apache/bin/outputlog.sh access" custom
ErrorLog "|/home/apache/bin/outputlog.sh error"
ここではオリジナルのshellにパイプしています。
outputlog.sh
# !/bin/sh
# ex) sh outputlog.sh access foobar.com
LOG_PATH=/var/log/httpd
ROTATELOGS_BIN=/etc/httpd/bin/rotatelogs
GZIP_SHELL=/home/apache/bin/gziplog.sh
if [ $# -le 0 ]; then
echo "args error.$#"
exit 1
fi
type=$1
domain=$2
case "$type" in
"access" | "access_log" ) type="access";;
"error" | "error_log" ) type="error";;
"transition" | "transition_log" ) type="transition";;
* ) exit 1;;
esac
# sg admin "$ROTATELOGS_BIN -cfl -p ${GZIP_SHELL} -L ${LOG_PATH}/${domain}${domain:+-}${type}_log ${LOG_PATH}/${type}/${domain}${domain:+-}${type}_log.%Y-%m-%d.%H:%M:%S 60"
sg admin "$ROTATELOGS_BIN -cfl -p ${GZIP_SHELL} -L ${LOG_PATH}/${domain}${domain:+-}${type}_log ${LOG_PATH}/${type}/${domain}${domain:+-}${type}_log.%Y-%m-%d 86400"
結果的には
sg admin "/etc/httpd/bin/rotatelogs -cfl -p /home/apache/bin/gziplog.sh \
-L /var/log/httpd/access_log /var/log/httpd/access/access_log.%Y-%m-%d 86400"
/var/log/httpd直下に現在進行形ログが残り
以下種類別のディレクトリに本体が置かれるようになっています。
ファイルの圧縮と削除
次に上記で指定している-p /home/apache/bin/gziplog.shの部分
gziplog.sh
# !/bin/sh
LOG_PATH=/var/log/httpd
GZIP_BIN=/bin/gzip
## Retention period.
TERM=7
## Ignore first.
if [ $# -le 1 ]; then
exit 0
fi
newfile=$1
oldfile=$2
### gzip ####
gzip ${oldfile} >/dev/null 2>&1
#### file delete ####
filename=${oldfile##*/}
find ${oldfile%/*} -maxdepth 1 -mtime +${TERM} -name "${filename%_log*}_log*.gz" -delete
公式にある通り、第二引数に切り替わる前のファイルのfullpathが来るので
それをそのままgzipしてます。
次にその引数からファイルの検索&削除
以上。