Skip to content

Instantly share code, notes, and snippets.

@abriening
Created January 2, 2013 12:49
Show Gist options
  • Save abriening/4434375 to your computer and use it in GitHub Desktop.
Save abriening/4434375 to your computer and use it in GitHub Desktop.
Reads a rails log and extracts the given parameters, prints CSV.
#!/usr/bin/env ruby
file = ARGV.shift
parameters = ARGV
unless file && !parameters.empty?
puts <<-end_usage
Usage #{$0} logfile param [param, ...]
Reads a rails log and extracts the given parameters, prints CSV.
Useful to include [action, controller] in parameters.
end_usage
exit
end
STDOUT.puts (%w[timestamp] + parameters).join(',')
pattern = %r!(^[\S]+)? .*Parameters.*"(?:#{parameters.join('|')})"!
File.open file do |f|
f.each do |line|
if line =~ pattern
row = [$1]
parameters.each do |attr|
line =~ %r!"#{attr}"=>"([^"]*)"!
row << $1
end
STDOUT.puts row.join(',')
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment