如何在 log4php 中动态更改日志文件名?
How to change log file name dynamically in log4php?
我是 log4php 的新手,我想动态更改日志文件名和路径。
我这样做了,但是 Test.log 消息中没有任何内容。
$crawlupLogger = Logger::getRootLogger();
$crawlupLogger->setLevel(LoggerLevel::toLevel(LoggerLevel::DEBUG));
$appender = new LoggerAppenderFile("MyAppender");
$appender->setFile("Test.log");
$appender->setAppend(false);
$appenderlayout = new LoggerLayoutPattern();
$pattern = '%date{U}%message%newline';
$appenderlayout->setConversionPattern($pattern);
$appender->setLayout($appenderlayout);
$appender->activateOptions();
$crawlupLogger->removeAllAppenders();
$crawlupLogger->addAppender($appender);
// Line head
$crawlupLogger->info("Date,Time,Level,Message");
这里是解决方案:
$crawlupLogger = Logger::getRootLogger();
$crawlupLogger->setLevel(LoggerLevel::toLevel(LoggerLevel::DEBUG));
$appender = new LoggerAppenderFile("CrawlUpLogAppender");
$appender->setFile("Test.log");
$appender->setAppend(false);
$appenderlayout = new LoggerLayoutPattern();
$pattern = "%date{U}\t%-5p\t%message%newline";
$appenderlayout->setConversionPattern($pattern);
$appenderlayout->activateOptions();
$appender->setLayout($appenderlayout);
$crawlupLogger->removeAllAppenders();
$crawlupLogger->addAppender($appender);
Rem : 注意使用 " 而不是 '。如果字符串用双引号 (") 括起来,PHP 将解释更多的转义序列特殊字符:
我是 log4php 的新手,我想动态更改日志文件名和路径。
我这样做了,但是 Test.log 消息中没有任何内容。
$crawlupLogger = Logger::getRootLogger();
$crawlupLogger->setLevel(LoggerLevel::toLevel(LoggerLevel::DEBUG));
$appender = new LoggerAppenderFile("MyAppender");
$appender->setFile("Test.log");
$appender->setAppend(false);
$appenderlayout = new LoggerLayoutPattern();
$pattern = '%date{U}%message%newline';
$appenderlayout->setConversionPattern($pattern);
$appender->setLayout($appenderlayout);
$appender->activateOptions();
$crawlupLogger->removeAllAppenders();
$crawlupLogger->addAppender($appender);
// Line head
$crawlupLogger->info("Date,Time,Level,Message");
这里是解决方案:
$crawlupLogger = Logger::getRootLogger();
$crawlupLogger->setLevel(LoggerLevel::toLevel(LoggerLevel::DEBUG));
$appender = new LoggerAppenderFile("CrawlUpLogAppender");
$appender->setFile("Test.log");
$appender->setAppend(false);
$appenderlayout = new LoggerLayoutPattern();
$pattern = "%date{U}\t%-5p\t%message%newline";
$appenderlayout->setConversionPattern($pattern);
$appenderlayout->activateOptions();
$appender->setLayout($appenderlayout);
$crawlupLogger->removeAllAppenders();
$crawlupLogger->addAppender($appender);
Rem : 注意使用 " 而不是 '。如果字符串用双引号 (") 括起来,PHP 将解释更多的转义序列特殊字符: