jump to navigation

Custom Logging in Ruby on Rails April 8, 2009

Posted by vyolian in development.
Tags: , ,
trackback

I’m one of those developers that likes to abuse standard output in development.  In my rails development, I would use ‘puts variable’ everywhere.  But sometimes, my debug print statements get clobbered with rail’s default logging of requests and SQL queries.

I’m aware there’s a debugger… but I find using puts quicker.  This is especially the case when I just want to see a pattern within an execution or loop rather than tracing variables at each step.

Anyway, here’s how to build a console window that’s devoted solely for debug print statements.  You may also find this useful for devoting a log file just to search, auditing, etc.  Here are the very basics:

Create Your Custom Logger

#custom_logger.rb
class CustomLogger < Logger
  def format_message(severity, timestamp, progname, msg)
    "#{msg}\n"
  end
end

logfile = File.open(RAILS_ROOT + '/log/custom.log', 'a')  #create log file
logfile.sync = true  #automatically flushes data to file
CUSTOM_LOGGER = CustomLogger.new(logfile)  #constant accessible anywhere

Load CustomLogger From Environment

#in development.rb
require "custom_logger"

Use CustomLogger

#in any controller, view or model
CUSTOM_LOGGER.info("info from custom logger")

Finally, create your debug console using
tail -f log/custom.log

Resources

http://maintainable.com/articles/rails_logging_tips (very thorough rails logging article)
http://www.themomorohoax.com/2009/02/09/use-debugger (rails debugger)
http://log4r.sourceforge.net/manual.html (log4r)

Comments»

1. Baijiu - May 19, 2009

Works like a charm. Well written article!

2. Srikanth - September 15, 2010

Awesome.. Working cool..

3. Laknath - October 31, 2010

Coupled with an initializer and a singleton this is even better. We can just CustomerLogger.logger.info “message”

4. Jonathan Leung - August 9, 2011

So I added to the code so that it will put out a Growl notification every time you hit a notification. You will need to have “require ‘ruby-growl’” in your gemfile. See http://segment7.net/projects/ruby/growl/ for details!

#custom_logger.rb
class CustomLogger < Logger
def format_message(severity, timestamp, progname, msg)
regex = /(([Ee][Rr][Rr][Oo][Rr])|())([^,]*)(,(.*))?/
match = regex.match(msg)
if match[1] != ""
error = true
subject = "ERROR: "
else
error = false
subject = ""
end

match[4].present? ? subject += match[4] : subject = "subject_error"
if match[6].present?
GROWL.notify "ruby-growl Notification", subject, match[6], 0, error
else
GROWL.notify "ruby-growl Notification", subject, "", 0, error
end

"[#{timestamp.strftime("%I:%M:%S%p")} JON] #{msg}\n\n"

end
end

logfile = File.open(RAILS_ROOT + '/log/custom.log', 'a') #create log file
logfile.sync = true #automatically flushes data to file
GROWL = Growl.new "127.0.0.1", "ruby-growl", ["ruby-growl Notification"], nil, "growl_password"
CUSTOM_LOGGER = CustomLogger.new(logfile) #constant accessible anywhere


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.