Skip to content

Instantly share code, notes, and snippets.

@Eugene-Shapovalov
Forked from olegykz/pretty_log_formatting.rb
Last active December 10, 2015 15:08
Show Gist options
  • Save Eugene-Shapovalov/4451901 to your computer and use it in GitHub Desktop.
Save Eugene-Shapovalov/4451901 to your computer and use it in GitHub Desktop.
class ActiveSupport::BufferedLogger
def formatter=(formatter)
@log.formatter = formatter
end
end
class Formatter
SEVERITY_TO_TAG_MAP = {'DEBUG'=>'meh', 'INFO'=>'fyi', 'WARN'=>'hmm', 'ERROR'=>'wtf', 'FATAL'=>'omg', 'UNKNOWN'=>'???'}
SEVERITY_TO_COLOR_MAP = {'DEBUG'=>'0;37', 'INFO'=>'32', 'WARN'=>'33', 'ERROR'=>'31', 'FATAL'=>'31', 'UNKNOWN'=>'37'}
PROTOCOL_COLOR = 34
USE_HUMOROUS_SEVERITIES = false
USE_COLOR_PROTOCOL = true
def call(severity, time, progname, msg)
if USE_HUMOROUS_SEVERITIES
formatted_severity = sprintf("%-3s","#{SEVERITY_TO_TAG_MAP[severity]}")
else
formatted_severity = sprintf("%-5s","#{severity}")
end
formatted_time = time.strftime("%Y-%m-%d %H:%M:%S.") << time.usec.to_s[0..2].rjust(3)
color = SEVERITY_TO_COLOR_MAP[severity]
unless msg.blank?
msg.gsub!(/POST|GET|PUT|DELETE/) {|r| "\033[#{PROTOCOL_COLOR}m#{r}\033[0m"} if USE_COLOR_PROTOCOL
"\033[0;37m#{formatted_time}\033[0m [\033[#{color}m#{formatted_severity}\033[0m] #{msg.strip} (pid:#{$$})\n"
end
end
end
Rails.logger.formatter = Formatter.new
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment