Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Gettign a logger with Log::Log4perl

by nikmit (Sexton)
on Oct 31, 2016 at 10:05 UTC ( [id://1175005]=perlquestion: print w/replies, xml ) Need Help??

nikmit has asked for the wisdom of the Perl Monks concerning the following question:

I have a daemon process which keeps a Dancer instance up, as well as periodically runs some tests in a child process.

That daemon is logging fine with log4perl, config as follows:

my $conf = q( log4perl.category.ess2_loader=DEBUG, LOGFILE log4perl.category.Tools=INFO, LOGFILE log4perl.appender.LOGFILE=Log::Log4perl::Appender::File log4perl.appender.LOGFILE.filename=/var/log/error.log log4perl.appender.LOGFILE.mode=append log4perl.appender.LOGFILE.layout=PatternLayout log4perl.appender.LOGFILE.layout.ConversionPattern=%d %P [%r] %F % +L %c - %m%n ); Log::Log4perl::init( \$conf );

In the daemon itself I get a logger with my $log = Log::Log4perl::get_logger("supervisor"); and that works as expected.

I can't get logging to work in modules, annoyingly with one exception - when the supervisor spawns a child to run tests, that child executes a module which calls a logger like my $log = Log::Log4perl::get_logger("Tools::run_tests"); That again works ok

Trying to repeat that in other modules, by adding a category in the main config and then getting a logger in the same way fails - I get an empty logger where only the category is defined.

I don't know what I'm missing, it probably is too obvious for me to get it...

Replies are listed 'Best First'.
Re: Gettign a logger with Log::Log4perl
by Anonymous Monk on Oct 31, 2016 at 10:51 UTC
    "Tools::run_tests" is not the same as "Tools"

    post 20 lines of code to show problem

      I would expect the code in Tools.pm to log, but it does not. Couldn't quite restrict it to 20 lines, it is as short as I could get it:

      the parent process:
      #!/usr/local/nik/ess2/perlbrew/perls/perl-5.22.2/bin/perl -w use strict; use lib '/testmod'; use Tools; use Log::Log4perl; my $conf = q( log4perl.category.ess2_loader=DEBUG, LOGFILE log4perl.category.Tools.run_test=INFO, LOGFILE log4perl.appender.LOGFILE=Log::Log4perl::Appender::File log4perl.appender.LOGFILE.filename=/testmod/debug.log log4perl.appender.LOGFILE.mode=append log4perl.appender.LOGFILE.layout=PatternLayout log4perl.appender.LOGFILE.layout.ConversionPattern=%d %P [%r] %F % +L %c - %m%n ); Log::Log4perl::init( \$conf ); my $log = Log::Log4perl::get_logger("ess2_loader"); $log->info("logging successfully"); run_monitor( { foo => 'bar', baz => 'foobar' } ); sub run_monitor { my $href = shift; $log->info("trying to spawn child for periodic check..."); if (my $ps = fork) { $log->info("successfully spawned PID $ps"); $SIG{CHLD} = "IGNORE"; } else { $log->info("running check as PID $$"); Tools::run_tests($href); } }

      The module called from the child:

      use warnings; use strict; package Tools; use Log::Log4perl; my $log = Log::Log4perl::get_logger("Tools::run_test"); my $href = shift; sub run_tests { foreach my $test (keys %{$href}) { $log->info("running test $test with parameters $href->{$test}" +); } exit; } 1;

      Whether I define the category as category.Tools or category.Tools.run_tests makes no difference in this example.

      To my understanding category settings propagate to sub-categories, unless these are defined with their own settings. That's why I originally defined the category as 'Tools', to cover all subroutines in Tools.pm

        The definition of $href is outside the sub run_tests(). Move it inside and you might get more ...
        use Data::Dumper; sub run_tests { my $href = shift; # by doubt always try to dump the data # print STDERR Dumper( $href ); foreach my $test (keys %{$href}) { $log->info("running test $test with parameters $href->{$test}" +); } exit; }
        Log by original code:
        2016/10/31 13:47:06 7732 [0] logger_1175005.pl 23 ess2_loader - loggin +g successfully 2016/10/31 13:47:06 7732 [0] logger_1175005.pl 29 ess2_loader - trying + to spawn child for periodic check... 2016/10/31 13:47:06 7732 [11] logger_1175005.pl 31 ess2_loader - succe +ssfully spawned PID -5796 2016/10/31 13:47:06 -5796 [11] logger_1175005.pl 34 ess2_loader - runn +ing check as PID -5796
        After var moved:
        2016/10/31 13:47:21 19276 [0] logger_1175005.pl 23 ess2_loader - loggi +ng successfully 2016/10/31 13:47:21 19276 [0] logger_1175005.pl 29 ess2_loader - tryin +g to spawn child for periodic check... 2016/10/31 13:47:21 19276 [14] logger_1175005.pl 31 ess2_loader - succ +essfully spawned PID -5168 2016/10/31 13:47:21 -5168 [14] logger_1175005.pl 34 ess2_loader - runn +ing check as PID -5168 2016/10/31 13:47:21 -5168 [19] Tools.pm 22 Tools.run_test - running te +st baz with parameters foobar 2016/10/31 13:47:21 -5168 [19] Tools.pm 22 Tools.run_test - running te +st foo with parameters bar

        Line 6 refers to Tools::run_test while the actual sub is run_tests. Based on your previous remark about ess_loader vs supervisor, perhaps this is just another typo, but if not it might be the source of the problem.

        As an aside, you should be able to copy-and-paste code into posts here. The introduction of typos is an unnecessary distraction, especially since they are often the real culprit.

        But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)

Re: Gettign a logger with Log::Log4perl
by nikmit (Sexton) on Oct 31, 2016 at 10:32 UTC
    The mismatch in categories ('ess2_loader' and 'supervisor') is only a mismatch in my post here - in my running code they match.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1175005]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2024-04-16 21:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found