I'm trying to dynamically add a STDOUT appender to a Log4perl logger and keep getting strange errors:
Odd number of elements in hash assignment at /opt/perl/lib/site_perl/5 +.16.3/Log/Log4perl/Appender/Screen.pm line 36. Use of uninitialized value in print at /opt/perl/lib/site_perl/5.16.3/ +Log/Log4perl/Appender/Screen.pm line 41. Odd number of elements in hash assignment at /opt/perl/lib/site_perl/5 +.16.3/Log/Log4perl/Appender/Screen.pm line 36. Use of uninitialized value in print at /opt/perl/lib/site_perl/5.16.3/ +Log/Log4perl/Appender/Screen.pm line 41.
which looks like the self object isn't getting passed in correctly to the Screen appender:
32 33 ################################################## 34 sub log { 35 ################################################## 36 my($self, %params) = @_; 37 38 if($self->{stderr}) { 39 print STDERR $params{message}; 40 } else { 41 print $params{message}; 42 } 43 }
my whole test looks like this:
use strict; use warnings; use Log::Log4perl; use Data::Dumper; my $logconfig = <<EOT; log4perl.appender.TEST=Log::Dispatch::FileRotate log4perl.appender.TEST.utf8=1 log4perl.appender.TEST.filename=/var/log/test.log log4perl.appender.TEST.mode=append log4perl.appender.TEST.size=1000000 log4perl.appender.TEST.max=10 log4perl.appender.TEST.layout=Log::Log4perl::Layout::PatternLayout log4perl.appender.TEST.layout.ConversionPattern=[%d{yyyy-MM-dd HH:mm:s +s.SSSSSS}] %m%n log4perl.appender.SCREENOUT=Log::Log4perl::Appender::Screen log4perl.appender.SCREENOUT.stderr=0 log4perl.appender.SCREENOUT.min_level=info log4perl.appender.SCREENOUT.layout=Log::Log4perl::Layout::PatternLayou +t log4perl.appender.SCREENOUT.layout.ConversionPattern=%m%n log4perl.rootLogger=INFO, SCREENOUT log4perl.logger.test=INFO, TEST EOT Log::Log4perl->init(\$logconfig); my $logger = Log::Log4perl->get_logger("test"); my $appender = Log::Log4perl->appender_by_name("SCREENOUT"); print Dumper($appender)."\n"; $logger->add_appender($appender); print Dumper($logger)."\n"; $logger->info("Test info message"); $logger->debug("Test debug message"); $logger->info("Test asdf123"); $logger->debug("Test debug adfklgnfgnsldfijgl");
It is working for the originally configured appender though because in test.log I see the correct lines:
[2017-11-28 15:58:59.126326] Test info message [2017-11-28 15:58:59.129663] Test asdf123
Any Ideas on what the issue might be / how to fix it? Thanks!

In reply to Log4perl add_appender issues by lost953

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.