Logging assist

A wrapper of Log4r-color to make it easy to add logging to your projects. This project was originally born from a need to make it easy to debug Rails 3 Generators. It is used extensively in the Cream framework

Install

gem install logging_assist

Usage

The default logger logs to a ColorOutputter with a default color scheme for each log level.

Setup

Inside any class you want to add logging

class MyClassToBeLogged
  include Rails3::Assist::BasicLogger
  ...
end  

Logging

You can use the methods #debug, #info, #warn, #error and #fatal

def my_method
  logger.debug 'wtf??'
  logger.info  "now inside #my_method"
end  

Add outputters

You can easily add one or more file outputters to the mix

class MyClassToBeLogged

  def initialize
    # setup logging file
    logger.add_logfile :logfile => 'here.log'
  end
end  

Custom outputters

For custom outputters (any supported by log4r) you can use logger.add_outputter(my_custom_outputter) Here my_custom_outputter must be an instance of a subclass of Log4r::Outputter

You can remove the outputter like this logger.remove_outputter(my_custom_outputter)

Advanced outputter configuration

Every logger also takes a :formatter and :level option. Use like this:

def initialize
  my_custom_formatter = PatternFormatter.new(:pattern => "[%l] %d :: %.40m")
  logger.add_logfile :logfile => 'here.log', :formatter => my_custom_formatter, :level => :info
end  

See PatternFormatter docs for more info on customizing the output formats

Currently the formatter and level of an outputter can only (easily) be set on creation when added.

Remove outputters

You can remove color output to the console like this:

class MyClassToBeLogged

  def initialize
    # setup logging file
    logger.remove_outputs :color
  end
end  

Remove file outputs logger.remove_outputs :file

Here The following convention: Log4r::[name]Outputter is used to find the types of Outputter to remove.

Define custom color schemes

A color scheme can be set for any log level like this

obj.logger.set_color :info, {:color => :green, :background => :white}

Please see colorize for more info. Use String.colors to see list of available colors.

Note on Patches/Pull Requests

  • Fork the project.
  • Make your feature addition or bug fix.
  • Add tests for it. This is important so I don’t break it in a future version unintentionally.
  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
  • Send me a pull request. Bonus points for topic branches.

Copyright

Copyright © 2010 Kristian Mandrup. See LICENSE for details.