|
From: Mike S. <m...@pe...> - 2008-10-30 18:19:26
|
On Thu, 30 Oct 2008, Erskine, Thomas (IT) wrote:
> The problem with setting a threshold on the appender to cause it not
> ot log is that I don't see a way to programmatically set the threshold
> to a specific level. It can be bumped up or down, but that requires
> the program to know where the threshold was set in the configuration.
> I.E. configuration in the program, which I'm trying to avoid.
It's a bit tricky because you need access to Log4perl's appender
wrapper, not the appender itself:
"The previously mentioned method "appender_by_name()" returns a refer-
rence 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." [perldoc Log::Log4perl]
So, if you have a disabled appender like
log4perl.appender.SCREEN=Log::Log4perl::Appender::Screen
log4perl.appender.SCREEN.layout = SimpleLayout
log4perl.appender.SCREEN.Threshold = OFF
in your configuration, you can retrieve its threshold setting via
my $app = $Log::Log4perl::Logger::APPENDER_BY_NAME{"SCREEN"};
my $threshold = $app->threshold();
and print it out like
print "Threshold set to ",
Log::Log4perl::Level::to_level( $threshold ),
"\n";
If you want to enable the appender, set
$threshold = $app->threshold( $ALL );
If you want to disable it again, set
$threshold = $app->threshold( $OFF );
-- Mike
Mike Schilli
m...@pe...
>
> The best I've been able to come up with is to define a logger with a
> threshold which won't log, call it deactivated, and assign this appender
> to it. Then to activate this appender, I can use appender_by_name(...)
> and get_root_logger()->add_appender(...). It took me a while to find it
> as get_root_logger is only defined on Log::Log4perl::Logger, not in
> Log::Log4perl.
>
> Thomas Erskine
> Consultant | Technology
> 2000 Barrington St | Suite 300 | Floor 04
> Halifax, NS B3J3K1
> Phone: +1 902 442-4709
> Tho...@mo...
>
>
>> -----Original Message-----
>> From: Mike Schilli [mailto:m...@pe...]
>> Sent: Wednesday, October 29, 2008 5:29 PM
>> To: Erskine, Thomas (IT)
>> Cc: log...@li...
>> Subject: Re: [log4perl-devel] activating appenders
>>
>> On Wed, 29 Oct 2008, Erskine, Thomas (IT) wrote:
>>
>>> I want to be able to define an appender in the log4perl
>> config file,
>>> but not activated and then activate it programmatically However,
>>> Log::Log4perl->appender_by_name('SCREENDEBUG') returns undef.
>>>
>>> I guess I could add SCREENDEBUG to the log4perl.logger line
>> and define
>>> it with a threshold which won't log anything and then bump the
>>> threshold up at runtime, but it seems wrong somehow.
>>
>> An appender that's not assigned to any logger in the
>> configuration is ignored by Log4perl at this point -- I think
>> that using a threshold, as you've suggested, is a perfectly
>> valid solution, though.
>>
>> Alternatively, you can define the appender programmatically
>> and add it to the logger via add_appender().
>>
>> -- Mike
>>
>> Mike Schilli
>> m...@pe...
>>
> --------------------------------------------------------
>
> NOTICE: If received in error, please destroy and notify sender. Sender does not intend to waive confidentiality or privilege. Use of this email is prohibited when received in error.
> |