Created
March 12, 2012 22:28
-
-
Save mepatterson/2025104 to your computer and use it in GitHub Desktop.
beautiful logging for Rails 3
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
| # create a file in initializers directory called "beautify_log.rb" (or whatever you want) and put in this code -- | |
| # | |
| # ActiveSupport patches | |
| # | |
| 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