Will Sheppard
Will Sheppard

Reputation: 3479

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

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?

Upvotes: 8

Views: 2439

Answers (2)

undefinedvariable
undefinedvariable

Reputation: 696

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});

Upvotes: 2

CanSpice
CanSpice

Reputation: 35808

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.

Upvotes: 11

Related Questions