/logging

A flexible logging library for use in Ruby programs based on the design of Java's log4j library.

Primary LanguageRuby

Logging

by Tim Pease <img src=“https://secure.travis-ci.org/TwP/logging.png”>

DESCRIPTION

Logging is a flexible logging library for use in Ruby programs based on the design of Java’s log4j library. It features a hierarchical logging system, custom level names, multiple output destinations per log event, custom formatting, and more.

INSTALL

gem install logging

EXAMPLE

This example configures a logger to output messages in a format similar to the core ruby Logger class. Only log messages that are warnings or higher will be logged.

require 'logging'

logger = Logging.logger(STDOUT)
logger.level = :warn

logger.debug "this debug message will not be output by the logger"
logger.warn "this is your last warning"

In this example, a single logger is created that will append to STDOUT and to a file. Only log messages that are informational or higher will be logged.

require 'logging'

logger = Logging.logger['example_logger']
logger.add_appenders(
    Logging.appenders.stdout,
    Logging.appenders.file('example.log')
)
logger.level = :info

logger.debug "this debug message will not be output by the logger"
logger.info "just some friendly advice"

The Logging library was created to allow each class in a program to have its own configurable logger. The logging level for a particular class can be changed independently of all other loggers in the system. This example shows the recommended way of accomplishing this.

require 'logging'

Logging.logger['FirstClass'].level = :warn
Logging.logger['SecondClass'].level = :debug

class FirstClass
  def initialize
    @logger = Logging.logger[self]
  end

  def some_method
    @logger.debug "some method was called on #{self.inspect}"
  end
end

class SecondClass
  def initialize
    @logger = Logging.logger[self]
  end

  def another_method
    @logger.debug "another method was called on #{self.inspect}"
  end
end

There are many more examples in the “examples” folder of the logging package. The recommended reading order is the following:

NOTES

Although Logging is intended to supersede Log4r, it is not a one-to-one replacement for the Log4r library. Most notably is the difference in namespaces – Logging vs. Log4r. Other differences include renaming Log4r::Outputter to Logging::Appender and renaming Log4r::Formatter to Logging::Layout. These changes were meant to bring the Logging class names more in line with the Log4j class names.

REQUIREMENTS

The Logging source code relies on the Mr Bones project for default rake tasks. You will need to install the Mr Bones gem if you want to build or test the logging gem.

gem install bones

After Mr Bones is installed you can install all the depdencies via the rake task.

rake gem:install_dependencies

Always remember that “rake -T” is your friend!

LICENSE

The MIT License

Copyright © 2012 Tim Pease

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.