Rails Illustrated

Rails, Web Design and the User Experience

A Simple Class Based Logger in Rails

In a project I was working on there were several classes that were involved in a lot of background processing. In order to monitor the behavior of this background processing I wanted a separate log file for these classes. The result: class_logger is a simple plugin that creates a separated log file for an ActiveRecord model.

Installation

./script/plugin install git://github.com/eandrejko/class_logger.git

Usage

In your model use

class Email < ActiveRecord::Base
    has_own_logger
end

which will by default create a log file log/email.log. To write to this log file use:

email = Email.create
email.log("sending")

or use the class method:

Email.class_logger("connecting to server")

Specifying the directory

If you want to keep the log file in another location specify the directory with:

class Email < ActiveRecord::Base
    has_own_logger :in => 'log/emails'
end

which will create the log file in log/emails/email.log.

Potential Applications

There are some advantages to having a separate log file. You can log many more events without cluttering up your production.log. This is potentially helpful for

  • troubleshooting background processing in production
  • recording and analyzing performance of long running processes in your application

Comments  

1

It's nice. Surely there is no dependency on ActiveRecord? What do you think about supporting log rotation?

  • log/email_20100105.log
  • log/payments_20100105.log

Stephan

Stephan Wehner wrote on January 5 2010
2

Stephan: You are correct, there is no dependency on ActiveRecord although RAILS_ROOT is used to determine the relative path of the log files.

It is easy to modify to support log rotations since the log file is written using the Ruby Logger class which supports rotating logs.

Erik wrote on January 6 2010

Add Comment

(required)
(required, won't be displayed)

(Use Markdown syntax)