0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

apache access_logのファイル名を可変にする方法

Last updated at Posted at 2017-10-25

access_logのファイル名を10分単位で任意のファイル名に変更したい場合どうすれば良いか調べた結果。

1. VirtualHost

<VirtualHost *:80>
  ServerName xxx.xxx
  SetEnvIf Request_URI \/favicon.ico$ no_log
  Logformat "%{%Y-%m-%d %T}t %A %{Client-Ip}i %{Referer}i %{User-Agent}i %D" common
  CustomLog "|/usr/local/bin/log.sh" common env=!no_log
</VirtualHost>

2. log.sh

#!/usr/bin/env bash
hostname=$(hostname)

while read line; do
  timestamp=$(date '+%s')
  prefix=$(($timestamp / 600))
  echo $line >> /var/log/access/${prefix}_foo_${hostname}
done

最初1つのログを処理してコマンドが終了する書き方をしていたら処理は動くけど以下のようなエラーが出るようになってしまったので上記のような継続的に標準入力を処理する方法に書き換える必要がありました。
apacheのログ出力は継続的にログを読み込むプロセスでなければいけないようですね。

AH00106: piped log program '/usr/local/bin/log.sh' failed unexpectedly
0
1
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?