23
23

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のアクセスログの形式をLTSVにしてみる

Last updated at Posted at 2013-02-19

やったこと

  • Apacheのconfig設定
# LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "host:%h\tident:%l\tuser:%u\ttime:%t\trequest-first-line:\"%r\"\tstatus:%>s\tresponse-size:%b\trefer:\"%{Referer}i\"\tuser-agent:\"%{User-Agent}i\"" ltsv

# CustomLog logs/access_log combined
CustomLog logs/access_log ltsv
  • Apacheのreload
$ sudo /etc/init.d/httpd reload
  • アクセスログの確認
  • 以下のような形式に
host:192.168.0.1	ident:-	user:-	time:[19/Feb/2013:12:41:50 +0000]	request-first-line:"GET /icons/small/folder2.gif HTTP/1.1"	status:404	response-size:342	refer:"http://www.example.com/icons/"	user-agent:"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17"
  • アクセスログを見やすく表示してみる
  • ltsview gemはruby1.8はサポートしておらず1.8では動作しなかった
$ sudo gem1.9 install ltsview

$ sudo tail /var/log/httpd/access_log | ltsview 
---
:host: 192.168.0.1
:ident: ! '-'
:user: ! '-'
:time: ! '[19/Feb/2013:12:41:52 +0000]'
:request-first-line: ! '"GET /favicon.ico HTTP/1.1"'
:status: '404'
:response-size: '330'
:refer: ! '"-"'
:user-agent: ! '"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.17
  (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17"'

$ sudo tail /var/log/httpd/access_log | ltsview -k host,user
:host: 192.168.0.1
:user: ! '-'
---
:host: 192.168.0.1
:user: ! '-'

$ sudo cat /var/log/httpd/access_log | ltsview -k host -r request-time:[0-9]\{6,\} 
# => request-timeが6桁以上の数値のものをフィルタリング
23
23
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
23
23

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?