rbala has asked for the wisdom of the Perl Monks concerning the following question:
I have a perl(test.pl) script which creates object for a module (say test.pm) and I use log4perl.pm in test.pl to get logs. My requirement is to add an appender file in test.pm such that two log files are created - one from test.pl and another from test.pm for whatever INFO messages given in both test.pl and test.pm.I use the below code. But what happens is , the log4perl initialisation is replaced by the config given in test.pm and hence only the INFO messages in test.pm gets logged. Is there any way to modify the log4perl configuration in test.pm while it is initialised in test.pl. I just want to have a shared access of Log4perl across modules. Please help.
test.pl --------
use TestModule; use Log::Log4perl qw(:easy); my $log_file = "somepath"; my %key_value_pairs = ( "log4perl.rootLogger" => "DEBUG, Logfile,TestModule,Screen", "log4perl.appender.Logfile" => "Log::Log4perl::Appender::File", + "log4perl.appender.Logfile.mode" => "clobber", "log4perl.appender.Logfile.filename" => "$log_file", + "log4perl.appender.Logfile.layout" => "Log::Log4perl::Layout::Pat +ternLayout", "log4perl.appender.Logfile.layout.ConversionPattern" => "%d %p %C: +%L> %m%n", "log4perl.appender.TestModule" => "Log::Log4perl::Appender::File +", "log4perl.appender.TestModule.mode" => "clobber", + "log4perl.appender.TestModule.filename" => "$log_file", + "log4perl.appender.TestModule.layout" => "Log::Log4perl::Layout:: +PatternLayout", "log4perl.appender.TestModule.layout.ConversionPattern" => "%d %p +%C:%L> %m%n", "log4perl.appender.Screen" => "Log::Log4perl::Appender::Screen", + "log4perl.appender.Screen.layout" => "Log::Log4perl::Layout::Patte +rnLayout", "log4perl.appender.Screen.layout.ConversionPattern" => "%d %p %C:% +L> %m%n", ); Log::Log4perl::init_once(\%key_value_pairs); INFO "Creating object for testmodule"; my $testmodule = TestModule->new( "key" => "value", );
package TestModule; use Log::Log4perl qw(:easy); use strict; use warnings; sub new { my ($this,%params) = @_; my $args = { "arg1" => "5", "arg2" => "6", %params, }; bless($args,$this); my $log_file = "somepath"; my $new_log_file = "somefilepath"; my %key_value_pairs = ( "log4perl.rootLogger" => "DEBUG, Logfile,TestModule,Screen", "log4perl.appender.Logfile" => "Log::Log4perl::Appender::File", + "log4perl.appender.Logfile.mode" => "clobber", "log4perl.appender.Logfile.filename" => "$log_file", + "log4perl.appender.Logfile.layout" => "Log::Log4perl::Layout::Patt +ernLayout", "log4perl.appender.Logfile.layout.ConversionPattern" => "%d %p %C: +%L> %m%n", "log4perl.appender.TestModule" => "Log::Log4perl::Appender::File +", "log4perl.appender.TestModule.mode" => "clobber", + "log4perl.appender.TestModule.filename" => "clobber", + "log4perl.appender.TestModule.layout" => "Log::Log4perl::Layout::P +atternLayout", "log4perl.appender.TestModule.layout.ConversionPattern" => "%d %p +%C:%L> %m%n", "log4perl.appender.Screen" => "Log::Log4perl::Appender::Screen +", "log4perl.appender.Screen.layout" => "Log::Log4perl::Layout::Patt +ernLayout", "log4perl.appender.Screen.layout.ConversionPattern" => "%d %p %C:% +L> %m%n", ); Log::Log4perl::init(\%key_value_pairs); INFO "New object created"; return $args; } 1;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Log4Perl across modules
by McA (Priest) on Sep 13, 2013 at 10:29 UTC | |
by rbala (Acolyte) on Sep 13, 2013 at 13:25 UTC | |
by rbala (Acolyte) on Sep 16, 2013 at 09:05 UTC |