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?


In reply to Log::Log4perl appender added at runtime doesn't receive messages by mcso

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.