LoginSignup
0
0

More than 5 years have passed since last update.

ApacheのログをYAMLにする

Last updated at Posted at 2019-03-17

概要

タイトルの通り、ApacheのログをYAMLで保存するという方法。半分ネタ的な投稿なので記事も短めに。

方法は簡単

httpd.confで、次のような設定をするだけ。あくまで一例なので、ログに残す項目はそれぞれの事情に合わせて設定すればOK。

httpd.conf
LogFormat "- time: !!timestamp %{%Y-%m-%dT%H:%M:%S}t.%{usec_frac}t%{%z}t\n  referer: \"%{Referer}i\"\n  protocol: %H\n  method: %m\n  path: \"%U%q\"\n  status: !!int %s\n  lastStatus: !!int %>s\n  file: \"%f\"\n  ipAddress: !!str %a\n  host: !!str %h\n  userAgent: \"%{User-agent}i\"" yaml
CustomLog "/somewhere/httpd.log.yml" yaml

そうすればログは・・・

httpd.log.yml
- time: !!timestamp 2019-03-17T10:00:00.123456+0900
  referer: "-"
  protocol: HTTP/2.0
  method: GET
  path: "/file?query"
  status: !!int 200
  lastStatus: !!int 200
  file: "/my/file"
  ipAddress: !!str FE80:0000:0000:0000:0202:B3FF:FE1E:8329
  host: !!str example.com
  userAgent: "MyWebBrowser/1.0"

という感じでYAMLになります1。そうすればログをparseするときにも、既存のYAMLパーサを使えばいいだけです2。便利。



  1. USER-AGENTなどに改行文字やダブルクオーテーションを入れられるとアウトですが、そんなことしてくるのは大体アウトローな輩でしょうから、そもそもアクセスさせないようにするのが良いでしょう。 ※ダブルクオーテーションはApacheがエスケープしてくれるみたいです。 

  2. 実は、このログではtimestampYAMLの標準に従っていないため、そこを良きに計らってくれるパーサでないとパースできないのでした…。Rubyのライブラリは大丈夫そうでした。求む、解決策。 

0
0
0

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
0