How can I disable Log4perl output for a particular class?
Asked Answered
T

2

5

I would like to use Log4perl in a project but disable it for a certain class (which is, in this case Net::Amazon). I thought this would be an easy one, but somehow I failed.

I tried using

use Log::Log4perl (:easy_init);
use Net::Amazon;

my $amz = Net::Amazon->new( ... );
my $log = Log::Log4perl->easy_init($DEBUG);
$log = $log->get_logger("Net::Amazon");
$log->level($OFF);

$log = $log->get_logger(__PACKAGE__);
$log->info("Hello World.");

Unfortunately, debugging messages of Net::Amazon are still printed to the terminal. Why is that? And what am I doing wrong here?

Taxeme answered 22/10, 2009 at 15:40 Comment(1)
At the end of your sample code you set $log back to the logger for PACKAGE, so we should expect "Hello world" to get logged. What happens if you move the $log->info() call back one line?Benavides
T
3

Problem solved, caused by human stupidity. Forgot to save the logger for the current package after getting it with get_logger. The following example works as expected:

use Log::Log4perl (:easy);
use Net::Amazon;

my $amz = Net::Amazon->new( ... );
Log::Log4perl->easy_init($DEBUG);
$log = get_logger("Net::Amazon");
$log->level($OFF);

$log = $log->get_logger(__PACKAGE__);
$log->info("Hello World.");
Taxeme answered 22/10, 2009 at 20:40 Comment(0)
B
4

I think you mean

    $log->get_logger("Net::Amazon")->level($OFF)
Benavides answered 22/10, 2009 at 17:5 Comment(1)
You are right, I accidentally forgot to save the logger before setting the level. I edited the post accordingly. Still, the problem persists.Taxeme
T
3

Problem solved, caused by human stupidity. Forgot to save the logger for the current package after getting it with get_logger. The following example works as expected:

use Log::Log4perl (:easy);
use Net::Amazon;

my $amz = Net::Amazon->new( ... );
Log::Log4perl->easy_init($DEBUG);
$log = get_logger("Net::Amazon");
$log->level($OFF);

$log = $log->get_logger(__PACKAGE__);
$log->info("Hello World.");
Taxeme answered 22/10, 2009 at 20:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.