NginxのログをLTSVフォーマットで出力する

社内向けのアプリを動かしているNginxのログフォーマットをLTSVに変更したのでメモ。

ログフォーマット追加
vi /usr/local/nginx/conf/nginx.conf
user nginx nginx;
worker_processes 2;
worker_priority 0;
worker_rlimit_nofile 8192;

error_log logs/error.log notice;
pid logs/nginx.pid;

events {
  multi_accept off;
  worker_connections 1024;
  use epoll;
}

http {
  include mime.types;
  default_type application/octet-stream;

  log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

  log_format ltsv "time:$time_iso8601"
                  "\thost:$remote_addr"
                  "\txff:$http_x_forwarded_for"
                  "\tmethod:$request_method"
                  "\tpath:$request_uri"
                  "\tstatus:$status"
                  "\tua:$http_user_agent"
                  "\treq_size:$request_length"
                  "\treq_time:$request_time"
                  "\tres_size:$bytes_sent"
                  "\tbody_size:$body_bytes_sent"
                  "\tapp_time:$upstream_response_time";

  sendfile on;
  server_tokens off;
  keepalive_timeout 10;
  index index.html index.htm;
  error_page 500 502 503 504 /50x.html;

  server {
    listen 80 default;
    server_name localhost;
    root html;
    access_log logs/access.log ltsv;

    location = /nginx_status {
      stub_status on;
      access_log off;
      allow 127.0.0.1;
      deny all;
    }

    location = /favicon.ico {
      log_not_found off;
    }
  }

  include conf.d/*.conf;
}
反映
/usr/local/nginx/sbin/nginx -t
mv -i /usr/local/nginx/logs/access.log{,.`date +%Y%m%d`}
kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
ps aux | grep nginx
tail /usr/local/nginx/logs/error.log
ruby -e '5.times{`curl http://localhost 2> /dev/null`}'
cat /usr/local/nginx/logs/access.log
rubyワンライナーで使ってみる
ruby -ne 'BEGIN{arr=[]};arr<<(Hash[$_.split("\t").map{|f|f.split(":")}])["res_size"].to_i;END{puts "total response size: #{arr.inject{|r,j|r+=j}}\nnumber of responses: #{arr.size}"}' /usr/local/nginx/logs/access.log