--- layout: post title: "Colorful logging in Rails 3.2" date: 2013-03-17 17:54 comments: true categories: [rails] cover: /images/cover/avatar.png keywords: ruby on rails, ruby, rails, logger, formatting, colors description: Formate and colorize log output in Rails --- The default Rails logger is really messy. Write somewhere ```logger.debug some_object.inspect``` and then for an hour search where the goddamn message is in a log file. Fortunately we can format and colorize ```logger``` output. Create ```config/initializers/log_formatting.rb``` file and paste this code: {% codeblock lang:ruby %}{% raw %} class ActiveSupport::BufferedLogger def formatter=(formatter) @log.formatter = formatter end end class Formatter SEVERITY_TO_COLOR_MAP = {'DEBUG'=>'33', 'INFO'=>'0;37', 'WARN'=>'36', 'ERROR'=>'31', 'FATAL'=>'31', 'UNKNOWN'=>'37'} def call(severity, time, progname, msg) formatted_severity = sprintf("%s","#{severity}") formatted_time = time.strftime("%Y-%m-%d %H:%M:%S.") << time.usec.to_s[0..2].rjust(3) color = SEVERITY_TO_COLOR_MAP[severity] if msg.empty? "\n" else "\033[0;37m#{formatted_time}\033[0m [#{formatted_severity}] \033[#{color}m#{msg.strip}\033[0m\n" end end end Rails.logger.formatter = Formatter.new {% endraw %}{% endcodeblock %} I found original code [here](http://cbpowell.wordpress.com/2012/04/05/beautiful-logging-for-ruby-on-rails-3-2/). I've just made small changes -- adjusted colors and formatting (and removed humorous stuff). This is how it looks in ```tail -f log/development.log```: {% img /images/rails-logger.png %}