开发者

log4php - Change log file Name dynamically in log4php.properties

开发者 https://www.devze.com 2022-12-27 18:32 出处:网络
hi how can i change the log file name and path inlog4php.properties dynamically log4php.app开发者_如何转开发ender.A8.File=../logs/logs.log

hi how can i change the log file name and path in log4php.properties dynamically

log4php.app开发者_如何转开发ender.A8.File=../logs/logs.log

Thanks


2 useful pieces of information:

(1) The previous answer by user367134 is helpful, however it has a bug: when setting the level you should not set it to the constant integer value denoted by LoggerLevel::DEBUG. You should instead make use of the LoggerLevel::toLevel() function to obtain a LoggerLevel object. i.e.,

$rootlogger->setLevel(LoggerLevel::DEBUG);

Should instead be:

$rootlogger->setLevel(LoggerLevel::toLevel(LoggerLevel::DEBUG));

(2) Here is a similar example to the one above, with a few differences:

  • uses rolling log files (max size of each log file is 100MB and at most 10 are kept)
  • uses a custom pattern for the log lines
  • fixes the setLevel bug
  • sets the log level at INFO

The code:

$rootlogger = Logger::getRootLogger();
$rootlogger->setLevel(LoggerLevel::toLevel(LoggerLevel::INFO));

$appender = new LoggerAppenderRollingFile("MyAppender");
$appender->setFile("custom_name.log", true);
$appender->setMaxBackupIndex(10); 
$appender->setMaxFileSize("100MB");
$appenderlayout = new LoggerLayoutPattern();
$pattern = '%d{Y-m-d H:i:s} [%p] %c: %m (at %F line %L)%n';
$appenderlayout->setConversionPattern($pattern);
$appender->setLayout($appenderlayout);
$appender->activateOptions();

$rootlogger->removeAllAppenders();
$rootlogger->addAppender($appender);

$rootlogger->info("info");


Well its not my code, But here is the sample code and link to the site

require_once('log4php/Logger.php');

  $rootlogger = Logger::getRootLogger();
  $rootlogger->setLevel(LoggerLevel::DEBUG);

  $appender = new LoggerAppenderFile("MyAppender");
  $appender->setFile("mylogfile.log", true);
  $appenderlayout = new LoggerLayoutTTCC();
  $appender->setLayout($appenderlayout);
  $appender->activateOptions();

  $rootlogger->removeAllAppenders();
  $rootlogger->addAppender($appender);

  $rootlogger->info("info");
  $rootlogger->error("error");
  $rootlogger->debug("debug");

Actual Site Link

Credit goes to "AKJOL"

0

精彩评论

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