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?

Will Sheppard
  • 3,272
  • 2
  • 31
  • 41

2 Answers2

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.

daxim
  • 39,270
  • 4
  • 65
  • 132
CanSpice
  • 34,814
  • 10
  • 72
  • 86
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});