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

Hi,

I'm trying to use log4perl and I want to log in a module which is called from multiple different script files. Irrespective of who the caller is, the module should log the message to that particular callers adapter/log file. Can this be achieved?


I'm trying to do the following.
test.pl
my $conf = q( + log4perl.logger.testLog = ).$logging_level.q(, logApp log4perl.appender.logApp= Log::Log4perl::Appender::File + log4perl.appender.logApp.mode= append log4perl.appender.logApp.syswrite= 1 log4perl.appender.logApp.filename= test.log + log4perl.appender.logApp.layout= Log::Log4perl::Layout::Pa +tternLayout log4perl.appender.logApp.layout.ConversionPattern = %m % +n ) Log::Log4perl->init( \$conf ); + use testPackage; + &testPackage::foo();
testPackage.pm
package testPackage; use Log::Log4perl qw(get_logger); sub foo { $logger = Log::Log4perl->get_logger(""); # IF I PUT testLog IN TH +E QUOTES THEN I'M GETTING THE blah IN THE CALLERS LOGS FILE $logger->info("blah"); } 1;
Can some monks please see this and bestow the light on this.

Replies are listed 'Best First'.
Re: log4perl in module appending to callers log file
by Khen1950fx (Canon) on Jul 28, 2011 at 04:21 UTC
    You were trying to go by the example in the doc, but you went a little too far afield. I fixed it up for you, but I can't really see what the problem is that you're having.
    #!/usr/bin/perl use strict; use warnings; use Log::Log4perl qw(get_logger); my $conf = q( + log4perl.category.testLog = INFO, logApp log4perl.appender.logApp = Log::Log4perl::Appender::File + log4perl.appender.logApp.mode = append log4perl.appender.logApp.filename = test.log log4perl.appender.logApp.layout = \ Log::Log4perl::Layout::PatternLayout log4perl.appender.logApp.layout.ConversionPattern - %m%n ); Log::Log4perl::init(\$conf); my $logger = get_logger('testLog'); $logger->error('This is better!');
Re: log4perl in module appending to callers log file
by wwe (Friar) on Jul 28, 2011 at 13:22 UTC
    may be you are missing to call get_logger in the main script. try this one:
    use strict; use warnings; use Log::Log4perl; my $logging_level = 'DEBUG'; my $conf = q( + log4perl.rootLogger = ).$logging_level.q(, logApp log4perl.appender.logApp = Log::Log4perl::Appender::File + log4perl.appender.logApp.mode = append log4perl.appender.logApp.filename = 917173_test.log + log4perl.appender.logApp.layout = Log::Log4perl::Layout::P +atternLayout log4perl.appender.logApp.layout.ConversionPattern = %m % +n ); Log::Log4perl->init( \$conf ); + my $logger = Log::Log4perl->get_logger(); $logger->info("outer"); testPackage::foo(); $logger->info("outer"); package testPackage; use Log::Log4perl qw(get_logger); sub foo { my $logger = Log::Log4perl->get_logger(); # IF I PUT testLog IN T +HE QUOTES THEN I'M GETTING THE blah IN THE CALLERS LOGS FILE $logger->info("inner"); } 1;
    this produces following output:
    outer inner outer
      Thanks for the reply. I wanted this behavior where called module logs to the caller script's appenders. The only logical difference I see from your code is that you have used rootLogger and I've used my logger.testLog. I'm also calling get_logger in the module but looks like calling get_logger("") in the module writes to rootLogger and not custom logger.

      And so I was not able to get the logs from the called module into the caller appenders.

      Do you know if there is any other way If I don't use the rootLogger and also doesn't specify the target logger name in the called module?
        I'm not a guru but this is a way I used Log4Perl. I don't know a reference but I think there is a logging hierarchy in other words there is a parent logger where all other logger depend on. And the logger on the top is a root logger. Maybe called modules look for this specific logger where they can append to. This config works fine for me so I don't care much about the thing behind it...