Skip to content

Instantly share code, notes, and snippets.

@Eugene-Shapovalov
Created January 4, 2013 11:35
Show Gist options
  • Save Eugene-Shapovalov/4451944 to your computer and use it in GitHub Desktop.
Save Eugene-Shapovalov/4451944 to your computer and use it in GitHub Desktop.
Rails versions < 3.2
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'}
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(:db)}\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