This page explains how to integrate multi-level logging in Ruby, using ruby's builtin Logger library
It also describes two extensions provided by utilrb
Ruby basic logger
The Ruby logger is a simple multi-level logging library
The API is documented here: http://ruby-doc.org/stdlib-1.8.7 (click on the Logger class for a quick usage summary)
Logger usage in Rock's ruby packages
The usage is to have at least a logger per library, that is available at the main namespace (i.e. Ruby module). For instance, the root logger in orocos.rb is
Orocos.logger
Utilrb provides an easy way to define this logger:
require 'utilrb/logger' module Orocos extend Logger::Root('orocos.rb', Logger::WARN) end
This does two things
- defines Orocos.logger and provides an initial setup for it. The program name and default logging level are provided as argument to Root
- provides forwarding methods from Orocos to Orocos.logger, i.e. one can do
Orocos.warn("message")
instead of
Orocos.logger.warn("message")
Hierarchical logger definition
Utilrb provides an extension that allows to share loggers in a hierarchical manner, for instance:
require 'utilrb/logger' module Orocos extend Logger::Root module Roby extend Logger::Hierarchy end end
Then, by default
Orocos::Roby.logger
will be equal to
Orocos.logger
i.e. if one does
Orocos::Roby.logger.level = Logger::INFO
Then the new level applies to Orocos.logger as well
To separate them, i.e. to create a separate logger at the level of Orocos::Roby simply do
Orocos::Roby.make_own_logger
The initially created logger will be setup the same way than Orocos.logger. You can then change that setup independently from the one in Orocos. For instance:
module Orocos extend Logger::Root('orocos.rb', Logger::WARN) module Roby extend Logger::Hierarchy logger.level = Logger::INFO # Both Orocos and Orocos::Roby have loggers at # Logger::INFO make_own_logger logger.level = Logger::DEBUG # Orocos.logger has a level of Logger::INFO and # Orocos::Roby.logger has a level of Logger::DEBUG end end
Additionally, a new progname and level can be given directly to make_own_logger:
module Orocos extend Logger::Root('orocos.rb', Logger::WARN) module Roby extend Logger::Hierarchy make_own_logger("Orocos::Roby", Logger::DEBUG) end end
The separated logger can be removed with
Orocos::Roby.reset_own_logger