Apache | ログファイル | アクセスログフォーマット(LogFormat)
アクセスログとして保存するデータは、直接カスタマイズして形式を指定できる。
ログ形式: LogFormat
ログ形式はLogFormatで指定する。文法は次のとおりである。
LogFormat "フォーマット" ログ形式名
デフォルトでhttpd.confファイルに記述されているログ形式としては、次の2つが定義されている。
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
ログ形式名にはcommonとcombinedの2つがある。形式は複数項目の組み合わせで定義され、設定可能な主な項目は次のとおりである。
| 設定値 | 意味 |
|---|---|
| %a | アクセスしたIPアドレス |
| %A | サーバー(Apache)のIPアドレス |
| %B | 転送されたバイト数(ヘッダーを除く) |
| %b | 転送されたバイト数(ヘッダーを除く)、0バイトの場合は-が表示される |
| %f | 要求されたファイル名 |
| %h | リモートホスト名 |
| %H | リクエストプロトコル名 |
| %l | クライアント識別子 |
| %m | リクエストメソッド名 |
| %q | リクエストに含まれるクエリ文字列。空でない場合は?が付く |
| %r | リクエストの最初の行の値 |
| %s | レスポンスステータス |
| %>S | 最終レスポンスのステータス |
| %t | 時刻 |
| %T | 処理にかかった時間 |
| %u | 認証ユーザー名 |
| %U | リクエストのURLパス |
| %v | リクエストに対する仮想ホスト名 |
| %V | UseCanonicalNameに基づくサーバー名 |
| %X | 接続状態 |
| %{クッキー名}C | リクエストに含まれるクッキー値 |
| %{環境変数名}e | 環境変数名の値 |
| %{ヘッダー名}i | リクエストに含まれるヘッダー名の値 |
| %{ヘッダー名}o | レスポンスに含まれるヘッダー名の値 |
| %{メモ}n | モジュールから返されたメモの値 |
| %{フォーマット}t | フォーマットされた時刻 |
これらの項目を列挙し、ダブルクォート(")で囲んで指定する。フォーマット内にダブルクォートを書く場合は、バックスラッシュ(\)でエスケープして記述する。
項目の中には中括弧({})で囲んで記述するものがある。たとえば、%{ヘッダー名}iは、ヘッダーの中から指定したヘッダー名の値を取得して記録する。%{User-Agent}iと指定した場合は、ヘッダーからUser-Agentの値を記録する。
アクセスログフォーマット変更テスト
デフォルトで有効になっているアクセスログはcommonである。もう1つ用意されているcombinedへ変更してみる。httpd.confファイル内に次のように記述された部分がある。
#
# The location and format of the access logfile (Common Logfile Format).
# If you do not define any access logfiles within a <VirtualHost>
# container, they will be logged here. Contrariwise, if you *do*
# define per-<VirtualHost> access logfiles, transactions will be
# logged therein and *not* in this file.
#
CustomLog "logs/access.log" common
#
# If you prefer a logfile with access, agent, and referer information
# (Combined Logfile Format) you can use the following directive.
#
#CustomLog "logs/access.log" combined
CustomLog logs/access.log commonの前に#を付け、代わりにCustomLog logs/access.log combinedの前の#を削除する。
#
# The location and format of the access logfile (Common Logfile Format).
# If you do not define any access logfiles within a <VirtualHost>
# container, they will be logged here. Contrariwise, if you *do*
# define per-<VirtualHost> access logfiles, transactions will be
# logged therein and *not* in this file.
#
#CustomLog "logs/access.log" common
#
# If you prefer a logfile with access, agent, and referer information
# (Combined Logfile Format) you can use the following directive.
#
CustomLog "logs/access.log" combined
これでアクセスログは、combinedという名前で定義された形式で保存される。Apacheを再起動した後、Apacheへ適当にアクセスしてログを確認する。
::1 - - [06/Dec/2019:00:48:42 +0900] "GET /admin/admin.html HTTP/1.1" 404 196 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"
127.0.0.1 - - [06/Dec/2019:00:49:03 +0900] "GET / HTTP/1.1" 200 46 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"
127.0.0.1 - - [06/Dec/2019:00:49:03 +0900] "GET /favicon.ico HTTP/1.1" 404 196 "http://127.0.0.1/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"
127.0.0.1 - - [06/Dec/2019:00:49:24 +0900] "GET /phpinfo HTTP/1.1" 404 196 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"
127.0.0.1 - - [06/Dec/2019:00:49:34 +0900] "GET /phpinfo.php HTTP/1.1" 200 72966 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"
上記が新しいフォーマットで記録されたログである。より詳細な情報が記録されていることが分かる。