Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

How to turn off a Log4perl appender?

by mifflin (Curate)
on Jul 17, 2006 at 20:19 UTC ( [id://561834]=perlquestion: print w/replies, xml ) Need Help??

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

I have a Log4perl configuration file that looks like...
log4perl.rootLogger=DEBUG, Screen, FileRotate log4perl.appender.Screen=Log::Log4perl::Appender::Screen log4perl.appender.Screen.stderr=0 log4perl.appender.Screen.layout=Log::Log4perl::Layout::PatternLayout log4perl.appender.Screen.layout.ConversionPattern = %d %-1.1p %P %H %m +%n log4perl.appender.FileRotate=Log::Dispatch::FileRotate log4perl.appender.FileRotate.filename=$logFileDir/$logFileName log4perl.appender.FileRotate.mode=append log4perl.appender.FileRotate.autoflush=1 log4perl.appender.FileRotate.DatePattern=yyyy-MM-dd-HH log4perl.appender.FileRotate.layout=Log::Log4perl::Layout::PatternLayo +ut log4perl.appender.FileRotate.layout.ConversionPattern = %d %-1.1p %P % +H %m%n
I know that I can have multiple configuration files and load different ones based on my application startup parameters. But I'd rather take a different approach.
Is it possible to programmatically turn off an appender?
For example, I would like to be able to turn off the Screen appender (and have only the FileRotate appender active) if the application is being started as a deamon.
Is this possible? I've been reading through the POD but can find nothing that might give me this capability.

Here is a snippet of code I would like to implement...
Log::Log4perl::init(\$log_conf); my $log = Log::Log4perl->get_logger; if ($opt{d}) { # if starting as a daemon... # turn off the Screen appender # ?? # change the log level use Log::Log4perl::Level; $log->level($INFO); }

Replies are listed 'Best First'.
Re: How to turn off a Log4perl appender?
by ptum (Priest) on Jul 17, 2006 at 21:23 UTC

    I have generally loaded only the appenders I wanted at startup ... but it seems to me (and this is untested) that you could use the warp_message property in Log::Log4perl::Appender to specify an on-the-fly subroutine that would suppress the appender (by returning nothing). Alternatively, you might be able to suppress the appender by nullifying its layout property. These both seem like hacks ... I'm hoping someone will answer with something a little more elegant.


    No good deed goes unpunished. -- (attributed to) Oscar Wilde
      There's an official way:
      It's also possible to remove appenders from a logger:
      
          $logger->remove_appender($appender_name);
      
      will remove an appender, specified by name, from a given
       logger. Please note that this does not remove an 
      appender from the system.
      
      To eradicate an appender from the system, you need to call
       Log::Log4perl->eradicate_appender($appender_name) 
      which will first remove the appender from every logger 
      in the system and then will delete all references 
      Log4perl holds to it.
      
        Thanks
        I've spent some time looking into these two methods for removing appenders but it turns out that you can only use them if you manually create the appenders (with Log::Log4perl::Appender->new) and add them (with $log->add_appender).
        If you create your appenders via a configuration file (like I've shown above) then the logger object does not keep track of all the appenders by name.
        I'm not sure if this is a 'feature' but it is the way it works with the version I have installed (1.05).
        Here is an example with a configuration file
        erickn@cofjora01d:/home/erickn> cat logtest1 use strict; use warnings; use Log::Log4perl; use Data::Dumper; my $log_conf1 = <<EOT; log4perl.rootLogger=DEBUG, Screen log4perl.appender.Screen=Log::Log4perl::Appender::Screen log4perl.appender.Screen.stderr=0 log4perl.appender.Screen.layout=Log::Log4perl::Layout::PatternLayout log4perl.appender.Screen.layout.ConversionPattern = %d %-1.1p %P %H %m +%n EOT Log::Log4perl->init(\$log_conf1); my $log = Log::Log4perl->get_logger; print Dumper $log; $log->debug('this is a test'); erickn@cofjora01d:/home/erickn> perl logtest1 $VAR1 = bless( { 'is_OFF' => sub { "DUMMY" }, 'is_DEBUG' => sub { "DUMMY" }, 'ERROR' => sub { "DUMMY" }, 'is_INFO' => sub { "DUMMY" }, 'layout' => undef, 'category' => 'main', 'DEBUG' => $VAR1->{'ERROR'}, 'is_ALL' => sub { "DUMMY" }, 'additivity' => 1, 'ALL' => sub { "DUMMY" }, 'is_FATAL' => sub { "DUMMY" }, 'is_WARN' => sub { "DUMMY" }, 'FATAL' => $VAR1->{'ERROR'}, 'appender_names' => [], 'WARN' => $VAR1->{'ERROR'}, 'INFO' => $VAR1->{'ERROR'}, 'level' => undef, 'num_appenders' => 0, 'OFF' => $VAR1->{'ERROR'}, 'is_ERROR' => sub { "DUMMY" } }, 'Log::Log4perl::Logger' ); 2006/07/17 18:05:13 D 14011 cofjora01d this is a test
        As you can see the object does not keep track of the appenders but still correctly writes output.

        However, if I manually create the appenders like...
        erickn@cofjora01d:/home/erickn> cat logtest2 use strict; use warnings; use Log::Log4perl; use Data::Dumper; my $screen = Log::Log4perl::Appender->new( "Log::Log4perl::Appender::Screen", name => "Screen", ); my $layout = Log::Log4perl::Layout::PatternLayout->new("%d %-1.1p %P % +H %m%n"); $screen->layout($layout); my $log = Log::Log4perl->get_logger; $log->add_appender($screen); print Dumper $log; $log->debug('this is another test'); erickn@cofjora01d:/home/erickn> perl logtest2 $VAR1 = bless( { 'is_OFF' => sub { "DUMMY" }, 'is_DEBUG' => sub { "DUMMY" }, 'ERROR' => sub { "DUMMY" }, 'is_INFO' => sub { "DUMMY" }, 'layout' => undef, 'category' => 'main', 'DEBUG' => $VAR1->{'ERROR'}, 'is_ALL' => sub { "DUMMY" }, 'additivity' => 1, 'ALL' => sub { "DUMMY" }, 'is_FATAL' => sub { "DUMMY" }, 'is_WARN' => sub { "DUMMY" }, 'FATAL' => $VAR1->{'ERROR'}, 'appender_names' => [ 'Screen' ], 'WARN' => $VAR1->{'ERROR'}, 'INFO' => $VAR1->{'ERROR'}, 'level' => undef, 'num_appenders' => 1, 'OFF' => $VAR1->{'ERROR'}, 'is_ERROR' => sub { "DUMMY" } }, 'Log::Log4perl::Logger' ); 2006/07/17 18:12:41 D 14290 cofjora01d this is another test
        The appender is listed in the object and is available for removal or eradication.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2024-04-25 06:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found