Last active
August 29, 2015 14:16
-
-
Save YanhaoYang/96714c339ff7f9720738 to your computer and use it in GitHub Desktop.
Parse rails log and generate structured data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "json" | |
requests = {} | |
File.open("production.log").each_line do |ln| | |
if ln =~ /^\[([0-9a-f]+)\] (.+)/ | |
id = $1 | |
msg = $2 | |
requests[id] ||= {lines: []} | |
requests[id][:lines] << msg | |
if msg =~ /Processing by (.+) as/ | |
requests[id][:controller] = $1 | |
end | |
if msg =~ /Started .+ for ([\d\.]+) at (.+)$/ | |
requests[id][:ip] = $1 | |
requests[id][:time] = $2 | |
end | |
#if msg =~ /chain halted as/ | |
#requests[id][:halted] = true | |
#end | |
#if msg =~ /CSRF token authenticity/ | |
#requests[id][:csrf] = true | |
#end | |
end | |
end | |
File.open("requests.txt", "w") do |f| | |
f.write(JSON.pretty_generate(requests)) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment