SYNOPSIS

  use Log::Dispatch::Config;
  Log::Dispatch::Config->configure('/path/to/log.conf');

  my $dispatcher = Log::Dispatch::Config->instance;
  $dispatcher->debug('this is debug message');
  $dispatcher->emergency('something *bad* happened!');

  # automatic reloading conf file, when modified
  Log::Dispatch::Config->configure_and_watch('/path/to/log.conf');

  # or if you write your own config parser:
  use Log::Dispatch::Configurator::XMLSimple;

  my $config = Log::Dispatch::Configurator::XMLSimple->new('log.xml');
  Log::Dispatch::Config->configure($config);

DESCRIPTION

Log::Dispatch::Config is a subclass of Log::Dispatch and provides a way to configure Log::Dispatch object with configulation file (default, in AppConfig format). I mean, this is log4j for Perl, not with all \s-1API\s0 compatibility though.

METHOD

This module has a class method \*(C`configure\*(C' which parses config file for later creation of the Log::Dispatch::Config singleton instance. (Actual construction of the object is done in the first \*(C`instance\*(C' call).

So, what you should do is call \*(C`configure\*(C' method once in somewhere (like \*(C`startup.pl\*(C' in mod_perl), then you can get configured dispatcher instance via \*(C`Log::Dispatch::Config->instance\*(C'.

CONFIGURATION

Here is an example of the config file:

dispatchers = file screen

file.class = Log::Dispatch::File file.min_level = debug file.filename = /path/to/log file.mode = append file.format = [%d] [%p] %m at %F line %L%n

screen.class = Log::Dispatch::Screen screen.min_level = info screen.stderr = 1 screen.format = %m

In this example, config file is written in AppConfig format. See Log::Dispatch::Configurator::AppConfig for details.

See \*(L"\s-1PLUGGABLE\s0 \s-1CONFIGURATOR\s0\*(R" for other config parsing scheme.

\s-1GLOBAL\s0 \s-1PARAMETERS\s0

dispatchers

dispatchers = file screen \*(C`dispatchers\*(C' defines logger names, which will be split by spaces. If this parameter is unset, no logging is done.

format

format = [%d] [%p] %m at %F line %L%n \*(C`format\*(C' defines log format. Possible conversions format are %d datetime string (ctime(3)) %p priority (debug, info, warning ...) %m message string %F filename %L line number %P package %n newline (\n) %% % itself Note that datetime (%d) format is configurable by passing \*(C`strftime\*(C' fmt in braket after %d. (I know it looks quite messy, but its compatible with Java Log4j ;) format = [%d{%Y%m%d}] %m # datetime is now strftime "%Y%m%d" If you have Time::Piece, this module uses its \*(C`strftime\*(C' implementation, otherwise \s-1POSIX\s0. \*(C`format\*(C' defined here would apply to all the log messages to dispatchers. This parameter is optional. See \*(L"\s-1CALLER\s0 \s-1STACK\s0\*(R" for details about package, line number and filename.

\s-1PARAMETERS\s0 \s-1FOR\s0 \s-1EACH\s0 \s-1DISPATCHER\s0

Parameters for each dispatcher should be prefixed with \*(L"name.\*(R", where \*(L"name\*(R" is the name of each one, defined in global \*(C`dispatchers\*(C' parameter.

You can also use \*(C`.ini\*(C' style grouping like:

[foo] class = Log::Dispatch::File min_level = debug

See Log::Dispatch::Configurator::AppConfig for details.

class

screen.class = Log::Dispatch::Screen \*(C`class\*(C' defines class name of Log::Dispatch subclasses. This parameter is essential.

format

screen.format = -- %m -- \*(C`format\*(C' defines log format which would be applied only to the dispatcher. Note that if you define global \*(C`format\*(C' also, %m is double formated (first global one, next each dispatcher one). This parameter is optional.

(others)

screen.min_level = info screen.stderr = 1 Other parameters would be passed to the each dispatcher construction. See Log::Dispatch::* manpage for the details.

SINGLETON

Declared \*(C`instance\*(C' method would make \*(C`Log::Dispatch::Config\*(C' class singleton, so multiple calls of \*(C`instance\*(C' will all result in returning same object.

my $one = Log::Dispatch::Config->instance; my $two = Log::Dispatch::Config->instance; # same as $one

See GoF Design Pattern book for Singleton Pattern.

But in practice, in persistent environment like mod_perl, lifetime of Singleton instance becomes sometimes messy. If you want to reload singleton object manually, call \*(C`reload\*(C' method.

Log::Dispatch::Config->reload;

And, if you want to reload object on the fly, as you edit \*(C`log.conf\*(C' or something like that, what you should do is to call \*(C`configure_and_watch\*(C' method on Log::Dispatch::Config instead of \*(C`configure\*(C'. Then \*(C`instance\*(C' call will check mtime of configuration file, and compares it with instanciation time of singleton object. If config file is newer than last instanciation, it will automatically reload object.

NAMESPACE COLLISION

If you use Log::Dispatch::Config in multiple projects on the same perl interpreter (like mod_perl), namespace collision would be a problem. Bizzare thing will happen when you call \*(C`Log::Dispatch::Config->configure\*(C' multiple times with differenct argument.

In such cases, what you should do is to define your own logger class.

package My::Logger; use Log::Dispatch::Config; use base qw(Log::Dispatch::Config);

Or make wrapper for it. See POE::Component::Logger implementation by Matt Sergeant.

PLUGGABLE CONFIGURATOR

If you pass filename to \*(C`configure\*(C' method call, this module handles the config file with AppConfig. You can change config parsing scheme by passing another pluggable configurator object.

Here is a way to declare new configurator class. The example below is hardwired version equivalent to the one above in \*(L"\s-1CONFIGURATION\s0\*(R".

  • Inherit from Log::Dispatch::Configurator. package Log::Dispatch::Configurator::Hardwired; use base qw(Log::Dispatch::Configurator); Declare your own \*(C`new\*(C' constructor. Stub \*(C`new\*(C' method is defined in Configurator base class, but you want to put parsing method in your own constructor. In this example, we just bless reference. Note that your object should be blessed hash. sub new { bless {}, shift }

  • Implement two required object methods \*(C`get_attrs_global\*(C' and \*(C`get_attrs\*(C'. \*(C`get_attrs_global\*(C' should return hash reference of global parameters. \*(C`dispatchers\*(C' should be an array reference of names of dispatchers. sub get_attrs_global { my $self = shift; return { format => undef, dispatchers => [ qw(file screen) ], }; } \*(C`get_attrs\*(C' accepts name of a dispatcher and should return hash reference of parameters associated with the dispatcher. sub get_attrs { my($self, $name) = @_; if ($name eq 'file') { return { class => 'Log::Dispatch::File', min_level => 'debug', filename => '/path/to/log', mode => 'append', format => '[%d] [%p] %m at %F line %L%n', }; } elsif ($name eq 'screen') { return { class => 'Log::Dispatch::Screen', min_level => 'info', stderr => 1, format => '%m', }; } else { die "invalid dispatcher name: $name"; } }

  • Implement optional \*(C`needs_reload\*(C' and \*(C`reload\*(C' methods. \*(C`needs_reload\*(C' should return boolean value if the object is stale and needs reloading itself. This method will be triggered when you configure logging object with \*(C`configure_and_watch\*(C' method. Stub config file mtime based \*(C`needs_reload\*(C' method is declared in Log::Dispatch::Configurator, so if your config class is based on filesystem files, you do not need to reimplement this. If you do not need singleton-ness at all, always return true. sub needs_reload { 1 } \*(C`reload\*(C' method should redo parsing of the config file. Configurator base class has a stub null \*(C`reload\*(C' method, so you should better override it. See Log::Dispatch::Configurator::AppConfig source code for details.

  • That's all. Now you can plug your own configurator (Hardwired) into Log::Dispatch::Config. What you should do is to pass configurator object to \*(C`configure\*(C' method call instead of config file name. use Log::Dispatch::Config; use Log::Dispatch::Configurator::Hardwired;

    my $config = Log::Dispatch::Configurator::Hardwired->new; Log::Dispatch::Config->configure($config);

CALLER STACK

When you call logging method from your subroutines / methods, caller stack would increase and thus you can't see where the log really comes from.

package Logger; my $Logger = Log::Dispatch::Config->instance;

sub logit { my($class, $level, $msg) = @_; $Logger->$level($msg); }

package main; Logger->logit('debug', 'foobar');

You can adjust package variable $Log::Dispatch::Config::CallerDepth to increase the caller stack depth. The default value is 0.

sub logit { my($class, $level, $msg) = @_; local $Log::Dispatch::Config::CallerDepth = 1; $Logger->$level($msg); }

Note that your log caller's namespace should not match against \*(C`/^Log::Dispatch/\*(C', which makes this module confusing.

AUTHOR

Tatsuhiko Miyagawa <[email protected]> with much help from Matt Sergeant <[email protected]>.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

RELATED TO Log::Dispatch::Config…

Log::Dispatch::Configurator::AppConfig, Log::Dispatch, AppConfig, POE::Component::Logger