LogIt

LogIt makes it easy to add custom logging to your Ruby or Rails application and has sensible formatting out of the box.

Usage

Minimal configuration

LogIt adds a class method to any including class to setup a logger method. For example:

require 'logit'

class Publisher
  include Logit

  logs_to :publisher

  def publish
    logger.info("doing publish")
    # do stuff...
  end
end

This will write logs to a publisher.log file in the current directory.

More config options

Writing to a specific directory

logs_to '/var/log/publishing/publisher.log'

Adding a progname to log entries

logs_to :publisher, :progname => "Publisher #{Process.pid}"

this will add something like ‘Publisher 1234’ to log entries.

Configuring log rotation

logs_to :publisher, :shift_age => 5, :shift_size => 102400

this will rotate logs up to a total of 5 files with max size of 102400 bytes.

Flushing each message to the log file immediately

logs_to :publisher, :flush_mode => :immediate

The default behavior is to use the default file buffering. Turning this on will cause each message to be written to the log file immediately. Alternatively, you can control this programmatically like so:

logger.info("my message")
logger.flush()

Also print to stdout

logs_to :publisher, :stdout => true

This will print log messages to stdout in addition to writing them to the log file.

Rails

LogIt automatically detects if you’re running in a Rails environment. If so, it will write to the Rails log directory and appends the environment to the log file name. For example:

RAILS_ROOT/log/publisher_development.log

Example log

09-12-2010 10:27:10 INFO   [publisher 4607]: Publishing files to endpoint.
09-12-2010 10:27:15 INFO   [publisher 4607]: Publishing completed.
09-12-2010 10:27:56 INFO   [publisher 4621]: Publishing files to endpoint.
09-12-2010 10:28:01 ERROR  [publisher 4621]: An exception occurred while publishing files: Connection reset by peer.
09-12-2010 10:28:32 INFO   [publisher 4634]: Publishing files to endpoint.
09-12-2010 10:28:32 WARN   [publisher 4634]: No files available to publish.

Installation

As a Gem

$ gem install logit

As a Rails plugin

$ script/plugin install git://github.com/ssayles/logit.git

Release Notes

1.0.2
  • Added :flush_mode option. Set this to :immediate if you want each message flushed to the log file immediately.

  • Added :stdout option. Set this to true if you want logs printed to stdout as well.

TODO

  • Allow users to pass in a proc or a name of a method to call to handle formatting of log entries.

  • Add some tests?

Credits

LogIt is written and maintained by Scott Sayles.

Copyright

Copyright © 2010 Scott Sayles. See LICENSE for details.