Skip to content

Instantly share code, notes, and snippets.

@YanhaoYang
Last active August 29, 2015 14:16
Show Gist options
  • Save YanhaoYang/96714c339ff7f9720738 to your computer and use it in GitHub Desktop.
Save YanhaoYang/96714c339ff7f9720738 to your computer and use it in GitHub Desktop.
Parse rails log and generate structured data
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