Railsのログファイルを検索する
やっつけ感がそこはかとないのだけど: select_rails_log.rb
使い方はこんな感じ。
コントローラとアクションを指定して抽出する。(アクションは省略可、カンマ区切りで複数指定可)
$ ruby ~/src/select_rails_log.rb log/production.log -a 'ItemsController#new'
time: 2018-02-08 15:55:48 +0900 .. 2018-02-08 15:55:48 +0900
id: 7592812d-b11f-46d0-9567-b4f287f37079
pid: 69371
status: 200
logs:
Processing by ItemsController#new as HTML
Rendering items/new.html.erb within layouts/application
Rendered items/_form.html.erb (6.5ms)
Rendered items/new.html.erb within layouts/application (11.2ms)
Completed 200 OK in 19ms (Views: 11.7ms | ActiveRecord: 0.2ms)
日時を指定して抽出する。(..
で期間を指定するか,
で前後n秒を指定する)
$ ruby ~/src/select_rails_log.rb log/production.log -t '2/8 15:55:48,3'
time: 2018-02-08 15:55:45 +0900 .. 2018-02-08 15:55:45 +0900
id: 169dda1a-021a-466d-8d21-a732f14a6edd
pid: 69371
status: 200
logs:
Processing by ItemsController#index as HTML
Rendering items/index.html.erb within layouts/application
Rendered items/index.html.erb within layouts/application (10.6ms)
Completed 200 OK in 21ms (Views: 15.1ms | ActiveRecord: 0.5ms)
time: 2018-02-08 15:55:48 +0900 .. 2018-02-08 15:55:48 +0900
id: 7592812d-b11f-46d0-9567-b4f287f37079
pid: 69371
status: 200
logs:
Processing by ItemsController#new as HTML
Rendering items/new.html.erb within layouts/application
Rendered items/_form.html.erb (6.5ms)
Rendered items/new.html.erb within layouts/application (11.2ms)
Completed 200 OK in 19ms (Views: 11.7ms | ActiveRecord: 0.2ms)
params(inspectされた文字列; eval注意)に対する正規表現マッチで抽出する。
$ ruby ~/src/select_rails_log.rb log/production.log -p '"id"=>"\d"'
time: 2018-02-08 15:55:58 +0900 .. 2018-02-08 15:55:58 +0900
id: 22d64347-7b16-4b04-96a9-60da747f7170
pid: 69371
status: 200
logs:
Processing by ItemsController#show as HTML
Parameters: {"id"=>"1"}
Rendering items/show.html.erb within layouts/application
Rendered items/show.html.erb within layouts/application (2.4ms)
Completed 200 OK in 9ms (Views: 3.0ms | ActiveRecord: 0.2ms)
抽出した中のParametersをevalしてppして表示する。(手抜き)
$ ruby ~/src/select_rails_log.rb log/production.log -p '"id"=>"\d"' -P
time: 2018-02-08 15:55:58 +0900 .. 2018-02-08 15:55:58 +0900
id: 22d64347-7b16-4b04-96a9-60da747f7170
pid: 69371
status: 200
params:
{"id"=>"1"}
logs:
Processing by ItemsController#show as HTML
Parameters: {"id"=>"1"}
Rendering items/show.html.erb within layouts/application
Rendered items/show.html.erb within layouts/application (2.4ms)
Completed 200 OK in 9ms (Views: 3.0ms | ActiveRecord: 0.2ms)
使い方。
usage: select_rails_log.rb [options] [log-files]
Options:
-a, --action-names=NAMES Filter by controller and action names; ex: "FooController#index,BarController,..."
-s, --statuses=STATUSES Filter by statuses; ex: "3,20,!201,..."
-t, --time-range=TIME_RANGE Filter by time range; ex: "2018-01-02 12:00..2018-02-01 12:00", "1/2 12:00...2/2 12:00", or "3/5,60"
-m, --method=METHOD Filter by HTTP method name
-p, --params-regexp=REGEXP Filter by parameters pattern; ex: '"foo"=>"ba[rz]"'
-r, --regexp=REGEXP Filter by pattern; ex: '"^ Rendering .*\.json"'
-v, --invert-match Invert match result
-D, --[no-]showx-debug-logs Show DEBUG logs
-P, --[no-]show-parsed-params Prityprint parameters (NOTE: use eval internally)
--raw Output as raw form
-h, --help Show help