Can Perl's Log::Log4perl's log levels be changed dynamically without updating config?
Asked Answered
O

2

8

I have a Mason template running under mod_perl, which is using Log::Log4perl.

I want to change the log level of a particular appender, but changing the config is too awkward, as it would have to pass through our deployment process to go live.

Is there a way to change the log level of an appender at run-time, after Apache has started, without changing the config file, and then have that change affect any new Apache threads?

Ottoottoman answered 10/5, 2011 at 16:5 Comment(0)
C
11

If you've imported the log level constants from Log::Log4perl::Level, then you can do things like:

$logger->level($ERROR); # one of DEBUG, INFO, WARN, ERROR, FATAL

$logger->more_logging($delta); # Increase log level by $delta levels,
                               # a positive integer

$logger->less_logging($delta); # Decrease log level by $delta levels.

This is in the Changing the Log Level on a Logger section in the Log::Log4perl docs.

Consolata answered 10/5, 2011 at 16:12 Comment(1)
Okay, but how would I specify the package I want to change the log level of?Ottoottoman
N
2

It seems kinda hacky to me, but it works:

$Log::Log4perl::Logger::APPENDER_BY_NAME{SCREEN}->threshold($DEBUG);

And to make it more dynamic, you could pass in a variable for the Appender name and level.

%LOG4PERL_LEVELS =
(
  OFF   =>$OFF,
  FATAL =>$FATAL,
  ERROR =>$ERROR,
  WARN  =>$WARN,
  INFO  =>$INFO,
  DEBUG =>$DEBUG,
  TRACE =>$TRACE,
  ALL   =>$ALL
);

$Log::Log4perl::Logger::APPENDER_BY_NAME{$appender_name}->threshold($LOG4PERL_LEVELS{$new_level});
Nomo answered 28/4, 2014 at 15:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.