Created
February 7, 2012 14:49
-
-
Save ChuckJHardy/1760041 to your computer and use it in GitHub Desktop.
New and Improved Logger for Rails
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
# config/initializers/buffered_logger.rb | |
module ActiveSupport | |
# Format the buffered logger with timestamp/severity info. | |
class BufferedLogger | |
NUMBER_TO_NAME_MAP = {0=>'DEBUG', 1=>'INFO', 2=>'WARN', 3=>'ERROR', 4=>'FATAL', 5=>'UNKNOWN'} | |
NUMBER_TO_COLOR_MAP = {0=>'0;37', 1=>'32', 2=>'33', 3=>'31', 4=>'31', 5=>'37'} | |
# http://api.rubyonrails.org/classes/Time.html#method-i-2D | |
DATE_FORMATTER = {0 => :db, 1 => :short, 2 => :long, 3 => :time} | |
def add(severity, message = nil, progname = nil, &block) | |
return if @level > severity | |
sevstring = NUMBER_TO_NAME_MAP[severity] | |
color = NUMBER_TO_COLOR_MAP[severity] | |
message = (message || (block && block.call) || progname).to_s | |
message = "\033[0;37m#{Time.now.to_s(DATE_FORMATTER[3])}\033[0m [\033[#{color}m" + sprintf("%-5s","#{sevstring}") + "\033[0m] #{message.strip} (pid:#{$$})\n" unless message[-1] == ?\n | |
buffer << message | |
auto_flush | |
message | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment