Skip to content

Instantly share code, notes, and snippets.

@johnnaegle
Last active December 21, 2015 01:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnnaegle/6225956 to your computer and use it in GitHub Desktop.
Save johnnaegle/6225956 to your computer and use it in GitHub Desktop.
A Rails 3.2 initializer that adds formatted time and process IDs to the log file
class ActiveSupport::BufferedLogger
def formatter=(formatter)
@log.formatter = formatter
end
end
class Formatter
SEVERITY_TO_COLOR_MAP = {'DEBUG'=>'0;37', 'INFO'=>'32', 'WARN'=>'33', 'ERROR'=>'31', 'FATAL'=>'31', 'UNKNOWN'=>'37'}
def call(severity, time, progname, msg)
formatted_severity = sprintf("%-5s","#{severity}")
formatted_time = time.strftime("%Y-%m-%d %H:%M:%S.") << time.usec.to_s[0..6].ljust(6)
color = SEVERITY_TO_COLOR_MAP[severity]
"\033[0;37m#{formatted_time} (pid:#{$$})\033[0m [\033[#{color}m#{formatted_severity}\033[0m] #{msg.strip}\n"
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