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

Re^2: How to turn off a Log4perl appender?

by saintmike (Vicar)
on Jul 17, 2006 at 23:04 UTC ( [id://561883]=note: print w/replies, xml ) Need Help??


in reply to Re: How to turn off a Log4perl appender?
in thread How to turn off a Log4perl appender?

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.
  • Comment on Re^2: How to turn off a Log4perl appender?

Replies are listed 'Best First'.
Re^3: How to turn off a Log4perl appender?
by mifflin (Curate) on Jul 18, 2006 at 01:16 UTC
    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.
      Ignoring the internals for now, isn't the following exactly what you're looking for?
      use strict; use warnings; use Log::Log4perl; my $log_conf1 = <<EOT; log4perl.rootLogger=DEBUG, Screen1, Screen2 log4perl.appender.Screen1=Log::Log4perl::Appender::Screen log4perl.appender.Screen1.stderr=0 log4perl.appender.Screen1.layout=Log::Log4perl::Layout::PatternLayout log4perl.appender.Screen1.layout.ConversionPattern = %d %-1.1p %P %H % +m %n log4perl.appender.Screen2=Log::Log4perl::Appender::Screen log4perl.appender.Screen2.stderr=0 log4perl.appender.Screen2.layout=Log::Log4perl::Layout::PatternLayout log4perl.appender.Screen2.layout.ConversionPattern = %d %-1.1p %P %H % +m %n EOT Log::Log4perl->init(\$log_conf1); Log::Log4perl->eradicate_appender("Screen1"); my $log = Log::Log4perl->get_logger; $log->debug('this is a test');
        yep , that works.
        I had been trying the instance method remove_appender and was getting the error message...
        C:\home\erickn>test.pl No such appender: Screen1 at :/Perl/site/lib/Log/Log4perl/Logger.pm l +ine 604.
        Which lead me into the internals.
        The class method eradicate_appender seems to work perfect.
        I'm going to have to revisit the pod to see if I can figure out the difference between the two.
      For anyone looking into this subject in the future, I found out today that the source of confusion is that appenders defined in a configuration file are stored within Log::Log4perl itself rather than in the relevant Log::Log4perl::Logger objects. The functions you need to get access to the appenders are primarily Log::Log4perl->appenders() and Log::Log4perl->appender_by_name().
      > 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). You can still access all the appenders with the Log::Log4perl class method appenders(), which returns a HASHREF to all the appenders that currently exist:
      Log::Log4perl->appenders()->{'Screen'}->threshold($OFF);
      will disable logging to the screen appender.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2024-04-19 09:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found