squid のアクセスログはデフォルトだと UNIX タイムで表示され、人間には「いつのログなのか?」を直感的に理解することが出来ません。 そこで、今回は squid のアクセスログを日付形式へ変更する方法をメモしておきます。
デフォルトで定義されている 5 種類のログフォーマット
squid のログフォーマットは Squid configuration directive logformat によるとデフォルトで以下の 5 種類が定義済み、と書かれています。
logformat squid %ts.%03tu %6tr %>a %Ss/%03>Hs %<st %rm %ru %[un %Sh/%<a %mt
logformat common %>a %[ui %[un [%tl] "%rm %ru HTTP/%rv" %>Hs %<st %Ss:%Sh
logformat combined %>a %[ui %[un [%tl] "%rm %ru HTTP/%rv" %>Hs %<st "%{Referer}>h" "%{User-Agent}>h" %Ss:%Sh
logformat referrer %ts.%03tu %>a %{Referer}>h %ru
logformat useragent %>a [%tl] "%{User-Agent}>h"
更に注釈として以下が書かれています。
NOTE: When the log_mime_hdrs directive is set to ON. The squid, common and combined formats have a safely encoded copy of the mime headers appended to each line within a pair of brackets.
NOTE: The common and combined formats are not quite true to the Apache definition. The logs from Squid contain an extra status and hierarchy code appended.
デフォルトのログ出力
明示的にログフォーマットを指定していない場合、squid のアクセスログ (/var/log/squid/access.log
) は以下のように UNIX タイムで表示されます。
1653444977.840 56 10.0.0.1 NONE_NONE/500 0 CONNECT www.google.com:443 - HIER_NONE/- -
ログフォーマットを日付形式にカスタマイズする
これを人間が理解しやすいように「日付形式」へ変更するには /etc/squid/squid.conf
へ以下のように定義します。
logformat datefmt %{%Y/%m/%d %H:%M:%S}tl.%03tu %6tr %>a %Ss/%03>Hs %<st %rm %ru %[un %Sh/%<a %mt
access_log daemon:/var/log/squid/access.log datefmt
squid を再起動して設定変更を反映します。
systemctl restart squid.service
これでアクセスログには以下のように表示されます。
2022/05/25 22:25:13.762 296 10.0.0.1 TCP_TUNNEL/200 5782 CONNECT www.google.com:443 - FIRSTUP_PARENT/192.168.1.1 -
squid の設定ファイルをチェックする
squid には squid -k [SIGNAL]
というコマンドがあります。 man
には以下のように書かれています。
-k reconfigure | rotate | shutdown | interrupt | kill | debug | check | parse
Parse configuration file, then send signal to running copy (except -k parse ) and exit.
check
というオプションを使い、squid -k check
と指定することで /etc/squid/squid.conf
の内容を確認することが出来ます。 設定に問題が無い場合は特に何も表示されません。
# squid -k check
#
設定に問題がある場合、以下のように該当箇所が指摘されます。
# squid -k check
2022/05/25 22:26:44| /etc/squid/squid.conf(35): unrecognized: 'BAD-CONFIG'
コメント