Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Log::Log4perl - changing an appenders threshold

by arc444 (Novice)
on Sep 07, 2017 at 11:18 UTC ( [id://1198853]=perlquestion: print w/replies, xml ) Need Help??

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

Hi. Im trying to change the thresold on an appender, but without success. Why do I want to do this ? - it would be in response to a '-debug' option provided to the script ( not shown for clarity ) Minimal case example of the issue is shown below. Suggestions very gladly welcomed Thank you.

#!/usr/bin/perl use strict; use warnings; use Log::Log4perl; my $log4p_conf = q( log4perl.rootLogger=DEBUG, SCREEN log4perl.appender.SCREEN=Log::Log4perl::Appender::Screen log4perl.appender.SCREEN.layout=SimpleLayout log4perl.appender.SCREEN.Threshold=INFO ); Log::Log4perl->init(\$log4p_conf); my $log = Log::Log4perl->get_logger(""); # This appears to have no effect : Log::Log4perl->appender_by_name('SCREEN')->threshold('DEBUG'); $log->fatal("fatal msg"); $log->error("error msg"); $log->warn("warn msg"); $log->info("info msg"); $log->debug("debug msg"); $log->trace("trace msg");

Replies are listed 'Best First'.
Re: Log::Log4perl - changing an appenders threshold
by Anonymous Monk on Sep 07, 2017 at 11:51 UTC
    Try calling method on $log

      Thanks for the suggestion, but there are no methods available on the $log object that will modify the appenders, only methods to adjust the logger itself.

      I have however now found a solution, to set the SCREEN appender to threshold of 'debug' : $Log::Log4perl::Logger::APPENDER_BY_NAME{'LOGFILE'}->threshold('DEBUG')

      This does work as I required.

      I believe that the method : Log::Log4perl->appender_by_name('SCREEN')->threshold('DEBUG') amends the 'actual appender' - however because these appenders are 'wrapped' it needs to be the 'wrapper class' itself that needs adjusted, to give me the desired result.

      I had achieved this initially by modifying the objects data directly using : ${$Log::Log4perl::Logger::APPENDER_BY_NAME{'SCREEN'}}{'level'} = 20000

      This is what initially seemed to be suggested at : http://log4perl.sourceforge.net/releases/Log-Log4perl/docs/html/Log/Log4perl.html#356d5 : "The previously mentioned method appender_by_name() returns a referrence to the real appender object. If you want access to the wrapper class (e.g. if you want to modify the appender's threshold), use the hash $Log::Log4perl::Logger::APPENDER_BY_NAME{...} instead, which holds references all appender wrapper objects."

      After re-reading a few times, Im now aware of the threshold method on the actual 'wrapper class' ( $Log::Log4perl::Logger::APPENDER_BY_NAME{'LOGFILE'} ) and can therefore also use the 'friendly name' ( ie DEBUG/TRACE/WARN ) rather than a numeric ( 30000/20000/5000 )

      Hope someone also finds this useful !

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (2)
As of 2024-04-26 00:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found