Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Log4Perl across modules

by McA (Priest)
on Sep 13, 2013 at 10:29 UTC ( #1053882=note: print w/replies, xml ) Need Help??


in reply to Log4Perl across modules

Hi,

Log::Log4perl follows the singleton pattern. There is always one instance of that class representing the logging facility. With init you initialize it. So, the second init just does what you see, it overwrites the first initialisation. What you want is to add an appender to the initially created object. Therefore you have to look at the runtime methods provides to do that. Have a look at Log::Log4perl::FAQ which gives very much hints how to do it.

UPDATE: Have a look at http://search.cpan.org/~mschilli/Log-Log4perl-1.42/lib/Log/Log4perl.pm#Advanced_configuration_within_Perl

McA

Replies are listed 'Best First'.
Re^2: Log4Perl across modules
by rbala (Acolyte) on Sep 13, 2013 at 13:25 UTC
    Thanks lot McA!! It seems to work in a test sample. I will proceed integrating it to my code. Thanks, Bala.
      Hi, The code works but the issue is even after I add appender file in my module.pm , it is logging only once until it is returned from the subrouting in the module. After that , whatever logging I put, it is appended to only the file , which I appended in script . Below is the code : test.pl -------
      use warnings; use strict; use Log::Log4perl qw(:easy); use TestModule; my $log_file = "GIVE SOME PATH"; 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::Patte +rnLayout", "log4perl.appender.Logfile.layout.ConversionPattern" => "%d %p %C: +%L> %m%n", "log4perl.appender.TestModule" => "Log::Log4perl::Appender::Fi +le", "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(\%key_value_pairs); my $testmodule = TestModule->new( "key" => "value", ); INFO "Creating object for testmodule"; $testmodule->_printTime();
      module.pm ---------
      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 = "GIVE SOME PATH"; my $file_appender = Log::Log4perl::Appender->new( "Log::Log4perl::Appender::File", name => "TestModule", filename => "$new_log_file"); INFO "New object created"; return $args; } sub _printTime { my $time = localtime; INFO "$time"; } 1;

      Actually , I need both the files to be updated once a new appender is added.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1053882]
help
Chatterbox?
[Corion]: But I have a very positive experience with a four day workweek and a three day weekend. I can't easily go back though to full money.
[Corion]: That is easy without having to pay for a house, a wife or children though. If I had any of these, or any two of these, the decision wouldn't be that easy.
[ambrus]: wait. I understand no wife and children, but how do you not have to pay for a house?
[hippo]: I had a low-paid job about 20 years ago and seriously considered going down to a 3-day week. Would have worked 60% of the time for about 80% of the cash.
[Corion]: ambrus: Well, I pay rent, but don't own a house with variable/ unforeseeable costs
[hippo]: The thesholds/benefits balance at the time was nuts.
[hippo]: But that doesn't last because, you know ... politicians. :(
[Corion]: hippo: Yeah, if you still get enough money to get by and don't have expensive hobbies or other fixed costs that can work out well
[ambrus]: ah good. I pay only rent too. but that still costs significant money.
[Corion]: ambrus: Yes, but that is already budgeted for

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (12)
As of 2017-09-21 15:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    During the recent solar eclipse, I:









    Results (249 votes). Check out past polls.

    Notices?