Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Log::Log4perl appender added at runtime doesn't receive messages

by mcso (Novice)
on May 13, 2016 at 13:38 UTC ( [id://1162955]=perlquestion: print w/replies, xml ) Need Help??

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

Hello monks!

I am playing around with Log::Log4perl for a project, and I need to add an appender at runtime, in addition to some default loaded from a configuration file. I can add an appender, and I can see that the appender is in the list, but it doesn't seem to get any of the messages.

Example script:
#!/usr/bin/perl use strict; use warnings; use Log::Log4perl; use Data::Dumper; Log::Log4perl::init('log4perl.conf'); my $logger = Log::Log4perl->get_logger(); $logger->info("Test before"); my $appender = Log::Log4perl::Appender::String->new("name" => 'string_ +appender'); my $pattern = Log::Log4perl::Layout::PatternLayout->new("[%p] %m%n"); $appender->layout($pattern); $logger->add_appender($appender); $logger->info("Test after"); print("APPENDER STRING: " . $appender->string() . "\n"); print("APPENDERS: " . Dumper(Log::Log4perl::appenders()) . "\n");
log4perl.conf
log4perl.rootLogger=INFO, logfile, stdout log4perl.appender.logfile=Log::Log4perl::Appender::File log4perl.appender.logfile.filename=example.log log4perl.appender.logfile.mode=append log4perl.appender.logfile.layout=PatternLayout log4perl.appender.logfile.layout.ConversionPattern=[%p] %m%n log4perl.appender.stdout=Log::Log4perl::Appender::Screen log4perl.appender.stdout.stderr=0 log4perl.appender.stdout.layout=PatternLayout log4perl.appender.stdout.layout.ConversionPattern=[%p] %m%n
Output:
>perl test.pl [INFO] Test before [INFO] Test after APPENDER STRING: APPENDERS: $VAR1 = <long dump of the three appenders: stdout, logfile, + and string_appender>

I have played around with a wide variety of ways to add the appender and getting the string, but I can't figure this one out. To make it slightly more fun, unless the solution really requires it, I am stuck with version 1.41 of Log::Log4perl.

Bonus question if there is a solution to the above:

If I were to have multiple threads, and each of them should have their own string appender which would not pick up logging from the other threads, a suggestion to how I should do that?

Replies are listed 'Best First'.
Re: Log::Log4perl appender added at runtime doesn't receive messages
by Anonymous Monk on May 13, 2016 at 23:41 UTC
      This worked perfectly, thank you!
Re: Log::Log4perl appender added at runtime doesn't receive messages
by Anonymous Monk on May 13, 2016 at 23:01 UTC
     log4perl.rootLogger=INFO, logfile, stdout doesn't mention an appender called "name"

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2024-04-23 16:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found