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:log4perl.conf#!/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");
Output: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
>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?
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |