Git Product home page Git Product logo

logging's Introduction

Logging

by Tim Pease

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.

Installation

gem install logging

Examples

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.level = :info

logger.add_appenders \
    Logging.appenders.stdout,
    Logging.appenders.file('example.log')

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:

Extending

The Logging framework is extensible via the little-plugger gem based plugin system. New appenders or formatters can be released as ruby gems. When installed locally, the Logging framework will automatically detect these gems as plugins and make them available for use.

The logging-email plugin is a good example to follow. It includes a lib/logging/plugins/email.rb file which is detected by the plugin framework. This file declares a Logging::Plugins::Email.initialize_email method that is called when the plugin is loaded.

The three steps for creating a plugin are:

  • create a new Ruby gem: logging-<name>
  • include a plugin file: lib/logging/plugins/<name>.rb
  • definie a plugin initializer: Logging::Plugins::<Name>.initialize_<name>

Development

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. Conveniently there is a bootstrap script that you can run to setup your development environment.

script/bootstrap

This will install the Mr Bones gem and the required Ruby gems for development. After this is done you can rake rake -T to see the available rake tasks.

License

The MIT License - see the LICENSE file for the full text.

logging's People

Contributors

twp avatar kares avatar noahsw avatar olleolleolle avatar bklang avatar pschrammel avatar thinkerbot avatar copiousfreetime avatar pkuczynski avatar petemounce avatar gdb avatar akarzim avatar japgolly avatar benlangfeld avatar charlietanksley avatar ggilder avatar chewi avatar martinos avatar mitchty avatar dentarg avatar reiz avatar greatgodapollo avatar thmzlt avatar

Watchers

James Cloos avatar Flávio Visetti avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.