I was searching for a logging library for ruby and the best I found was this.

Log4r is a logging library for ruby. I have confirmed that it is working, out of the box, with many processes (including daemon processes) all logging to the same file.

There is much more to it, than it is written here. But for the basic purpose of logging this should be sufficient.

Log4r Homepage
Very useful short manual (contains all of the below and a bit more)

require 'log4r'
include Log4r

# create a logger named 'mylog' that logs to stdout
mylog = 'mylog'
# mylog can be kept global for the whole project

# This will make Log4r output to a file
mylog.outputters =
      { :filename => 'log_file.log',
        :trunc => true})
# if trunc is true, truncates the file each time,
# useful when debugging and running many times
# (files don't grow too large)

# As well one can output to screen
mylog.outputters = Outputter.stdout

Once the logger has been created it can be used like this:

  mylog.debug "This is a message with level DEBUG" "This is a message with level INFO"
  mylog.warn "This is a message with level WARN"
  mylog.error "This is a message with level ERROR"
  mylog.fatal "This is a message with level FATAL"

One can say that all messages below some level (for example ERROR) should not be displayed.

  mylog.level = ERROR

The level hierarchy goes like this:

One really good tip was found by one of my colleagues: if you really really care about effieciency, you could avoid creating the String objects used as log messages if the logging is disabled at this level by wrapping it in a closure: {"This is a message with level INFO"}

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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