Created
December 6, 2011 15:40
-
-
Save andruby/1438620 to your computer and use it in GitHub Desktop.
Fancy Debug
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
class ActiveSupport::BufferedLogger | |
# Print a logger.debug line with colored Name | |
# If the msg is not a string, it will inspect it | |
# Pass it a block to record the time_taken to yield it | |
# Symbols in the msg like :method will be interpolated with block_result.send(:method) | |
# eg: fancy_debug("Find Users", "Found :count users") { User.where() } | |
def fancy_debug(name, msg = '', &block) | |
if block_given? | |
start_time = Time.now | |
result = yield | |
if msg.is_a?(String) | |
msg.scan(/:[a-z_]+/) do |method| | |
msg.gsub!(":#{method}", result.send(method.to_sym).to_s) | |
end | |
else | |
msg = msg.inspect | |
end | |
time = ((Time.now-start_time) * 1000).round(2) | |
Rails.logger.debug(" \e[31m#{name} (#{"%7.2f" % time}ms)\e[0m #{msg}") | |
return result | |
else | |
Rails.logger.debug(" \e[31m#{name}\e[0m #{msg}") | |
return true | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment