开发者

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开发者_如何学C 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?


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜