log4php - 在 log4php.properties 中动态更改日志文件名称

发布于 2024-08-29 13:37:41 字数 107 浏览 7 评论 0原文

您好,如何动态更改 log4php.properties 中的日志文件名和路径

log4php.appender.A8.File=../logs/logs.log

谢谢

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

log4php.appender.A8.File=../logs/logs.log

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

与往事干杯 2024-09-05 13:37:41

2 条有用的信息:

(1)user367134 之前的回答很有帮助,但它有一个错误:设置级别时,不应将其设置为 LoggerLevel::DEBUG 表示的常量整数值。您应该使用 LoggerLevel::toLevel() 函数来获取 LoggerLevel 对象。
即,

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

应该是:

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

(2) 这是与上面的示例类似的示例,但有一些差异:

  • 使用滚动日志文件(每个日志文件的最大大小为 100MB,最多保留 10 个)
  • 使用自定义模式loglines
  • 修复了setLevel错误
  • 将日志级别设置为INFO

代码:

$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");

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");
客…行舟 2024-09-05 13:37:41

好吧,这不是我的代码,但这里是示例代码和网站链接

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

实际网站链接

归功于“AKJOL”

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"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文