すべての apache ログを CSV ファイルとして保存する方法はありますか?
access.log->access_log.csv
error.log->error_log.csv
すべての apache ログを CSV ファイルとして保存する方法はありますか?
access.log->access_log.csv
error.log->error_log.csv
過去に書き込まれたログファイルや、設定ファイルにアクセスできない apache サーバーからのログファイルを見たいという問題がある場合、または何らかの理由でログファイル形式を変更したくない場合:
デフォルトのApacheログファイルをlibre office calcで読み取れる形式に変換する小さなLinuxシェルsedスクリプトを作成しました。
#!/bin/bash
#reformat apache's access logs, so that they can be interpreted as csv files,
# with space as column delimiter and double quotes to bind together things
# that contain spaces but represent single columns.
# 1) add a doublequote at the begining of the line. first column is the ip adress.
# ip-adresses that have 3 digits in every group but the first could be interpreted as numbers
# with the dots marking groups of thousands.
# 2a) end the ip-adress with quotes
# 2b) surround the second (to me unknown) column thats always just "-" and the
# third column which is the username with quotes
# 2c) reformat the date from "[09/Jul/2012:11:17:47" to "09.Jul 2012 11:17:47"
# 3) remove the string "+0200]" (replace it with doublequotes to end the date column)
# 4) the string that contains the command (5th column) sometimes contains string representation
# of binary rubish. thats no problem as long as this does not contain a doublequote which
# will mess up the column zoning. According to my web searches, csv columns should allow to
# contain doublequotes if they are escaped with a backslash. Although this is the case with
# these problematic strings, Libre Office does not accept it that way. therefore we escape every
# doublequote with a doubleqoute, which is the other valid option according to csv specifications,
# and libre office does accept that one. More technical: we replace every doublequote that does
# neither have a space or another doublequote before it, neither after it, with two doublequotes.
sed \
-e 's/^/"/' \
-e 's/ \([^ ]\{1,\}\) \([^ ]\{1,\}\) \[\([0-9]\{1,2\}\)\/\([a-zA-Z]\{1,3\}\)\/\([0-9]\{1,4\}\):/" "\1" "\2" "\3.\4 \5 /' \
-e 's/ +0200\] /" /' \
-e 's/\([^" ]\)"\([^" ]\)/\1""\2/g'
カスタム ログ形式を定義して、Apache ログを直接コンマ区切り形式にすることができます。
正しい方法を見つけるために、しばらくこれをいじる必要があるかもしれません。たとえば、フィールド値内のコンマが CSV を壊すのを防ぐために、フィールド区切り文字として"
orを使用することをお勧めします。'