On Tue, 31 Mar 2009, Ben Humphreys wrote:

> I want a default base set up for the logs, for formatting and levels
> of explicit-ness, but I also want users to be able to override certain
> parts of this behaviour with config files.
> I can imagine doing the default behaviour in Perl, and then loading the
> config file, but I'm not sure if that will trash all the default settings.
> Or there is the string merge idea from the link above, but that seems a
> little tricky.

If you want to make sure that your settings have precedence, you need to
load the user configuration file first and then make the corrections.
The other way around, their configuration will override yours.

One way to 'merge' two configurations to use the parse() method of the
Log::Log4perl::Config::PropertyConfigurator class:

     use Data::Dump qw(dump);
     use Log::Log4perl::Config;
     use Log::Log4perl::Config::PropertyConfigurator;
         my $conf = Log::Log4perl::Config::PropertyConfigurator->new();
         $conf->file("l4p.conf");
         my $data = $conf->parse(); # will die() on error
     print dump($data);

After that (and note that parse() will die() if there's an error, so you
might want to use an eval {} around it), $data will contain a data
structure that you then can modify and merge with another one obtained
from a second file.

Once you have a satisfactory data structure in $data, you can pass it as
the second (!) argument to Log4perl's init function:

     Log::Log4perl->init( undef, $data );

Highly undocumented feature ;).

Hope that helps!

-- Mike

Mike Schilli
m...@perlmeister.com

------------------------------------------------------------------------------
_______________________________________________
log4perl-devel mailing list
log4perl-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/log4perl-devel

Reply via email to