How to change log file name dynamically in log4php?

时间:2016-05-17 11:19:07

标签: log4php

I am new to log4php and I would like to change the log file name and path dynamically.

I do this, but there is nothing in Test.log message.

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

1 个答案:

答案 0 :(得分:0)

这里是解决方案:

$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将解释特殊字符的更多转义序列: