Created
January 24, 2011 21:27
-
-
Save esmarkowski/793997 to your computer and use it in GitHub Desktop.
beautiful colorized messages.
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
module ActiveSupport | |
class BufferedLogger | |
module Severity | |
COLORIZE = 10 | |
end | |
include Severity | |
def add(severity, message = nil, progname = nil, &block) | |
return if @level > severity | |
message = (message || (block && block.call) || progname).to_s | |
message = "\e[0;45m#{message}\e[0;0m\n" if severity == 10 | |
# If a newline is necessary then create a new message ending with a newline. | |
# Ensures that the original message is not mutated. | |
message = "#{message}\n" unless message[-1] == ?\n | |
buffer << message | |
auto_flush | |
message | |
end | |
for severity in Severity.constants | |
class_eval <<-EOT, __FILE__, __LINE__ + 1 | |
def #{severity.downcase}(message = nil, progname = nil, &block) # def debug(message = nil, progname = nil, &block) | |
add(#{severity}, message, progname, &block) # add(DEBUG, message, progname, &block) | |
end # end | |
def #{severity.downcase}? # def debug? | |
#{severity} >= @level # DEBUG >= @level | |
end # end | |
EOT | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment