De-ASCII Your Rails Logs
Written by Jared Haworth on
Recently, I've been delving into some filter-related problems in one of my Rails applications. This has required me to trap and review specific segments of my Rails log files. As part of my initial stack setup when I begin a new project, I install the Query Analyzer and Query Trace plugins.
The upside is I get very detailed trace information for my application, pictured below:
The downside is, when these files are viewed with Textmate or Console, they wind up looking more like this:
Rendered events/_event (0.00036)
[4;35;1mSlot Load (0.000361)[0m [0mSELECT * FROM 'slots' WHERE (slots.event_id = 23) [0m
[35;2mvendor/plugins/query_analyzer/lib/query_analyzer.rb:38:in 'select'[0m
[35;2mlib/association_extensions/chronological.rb:9:in 'first'[0m
[35;2mapp/models/event.rb:109:in 'begin_at'[0m
[35;2mapp/views/events/_event.html.erb:6:in '_run_erb_47app47views47events47_event46html46erb'[0m
[35;2mapp/views/events/index.html.erb:39:in '_run_erb_47app47views47events47index46html46erb'[0m
Today's "stretch the brain" exercise centered around creating a Textmate command to clear out this unnecessary cruft. For starters, I cloned the Text bundle's "Remove Unprintable Characters in Document / Selection" command. It looks like the Textmate folks are using Perl to remove unprintable characters:
perl -pe 's/[^\t\n\x20-\xFF]|\x7F|\xC2[\x80-\x9F]//g'
I left all the settings unchanged:
Save: Nothing
Input: Selected Text or Document
Output: Replace Selected Text
And edited the Perl regular expression to match each of the three possible variants of ASCII instruction:
perl -pe 's/\[\d;?(\d+)?;?(\d)?m//g'
Success! Now, with one keystroke, I can remove all that extra information from my log file, and get down to business.