0

我使用 log4php 将分号分隔的值记录到 csv 文件中。我想在第一行写一个标题。如何做到这一点?

例子

消息 ID、文件名、开始时间、结束时间、状态 MSGID1000;SAMPLE.TXT;01:05:55;07:08:44;OK MSGID1000;SAMPLE.TXT;01:05:55;07:08:44;挪威克朗

我这样做

csvLogger = Logger::getRootLogger();
csvLogger->setLevel(LoggerLevel::toLevel(LoggerLevel::DEBUG));
csvLogger->removeAllAppenders();

$appender = new LoggerAppenderFile("csvLogAppender");
$appender->setFile("LogMessage.csv");
$appender->setAppend(false);   

csvLogger->addAppender($appender); 
4

1 回答 1

0

我找到了扩展LoggerLayoutPattern和覆盖该getHeader()方法的解决方案。

class csvLoggerLayoutPattern extends LoggerLayoutPattern {
    public function getHeader() {
        return "message id, file name, start time, end time, status\n";
    }
}

csvLogger = Logger::getRootLogger();
csvLogger->setLevel(LoggerLevel::toLevel(LoggerLevel::DEBUG));
csvLogger->removeAllAppenders();

$appender = new LoggerAppenderFile("csvLogAppender");
$appender->setFile("LogMessage.csv");
$appender->setAppend(false);

$appenderlayout = new csvLoggerLayoutPattern();
$appenderlayout->activateOptions();
$appender->setLayout($appenderlayout);

csvLogger->addAppender($appender);
于 2016-06-04T07:42:04.287 回答