Created
June 26, 2012 17:25
-
-
Save rgould/2997230 to your computer and use it in GitHub Desktop.
Print out sql queries when they happen.
This file contains 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
ANSI = {} | |
ANSI[:RESET] = "\e[0m" | |
ANSI[:BOLD] = "\e[1m" | |
ANSI[:UNDERLINE] = "\e[4m" | |
ANSI[:LGRAY] = "\e[0;37m" | |
ANSI[:GRAY] = "\e[1;30m" | |
ANSI[:RED] = "\e[31m" | |
ANSI[:GREEN] = "\e[32m" | |
ANSI[:YELLOW] = "\e[33m" | |
ANSI[:BLUE] = "\e[34m" | |
ANSI[:MAGENTA] = "\e[35m" | |
ANSI[:CYAN] = "\e[36m" | |
ANSI[:WHITE] = "\e[37m" | |
$odd_or_even_queries = false | |
ActiveSupport::Notifications.subscribe('sql.active_record') do |*args| | |
$odd_or_even_queries = !$odd_or_even_queries | |
color = $odd_or_even_queries ? ANSI[:CYAN] : ANSI[:MAGENTA] | |
event = ActiveSupport::Notifications::Event.new(*args) | |
time = "%.1fms" % event.duration | |
name = event.payload[:name] | |
sql = event.payload[:sql].gsub("\n", " ").squeeze(" ") | |
puts " #{ANSI[:UNDERLINE]}#{color}#{name} (#{time})#{ANSI[:RESET]} #{sql}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment