duanji1610 2016-06-04 06:37
浏览 32
已采纳

使用log4php将标头写入日志

I have semi-colon separated values logged to a csv file using log4php. I wanted to write a header in first line. How this can be done?

Example

message id, file name, start time, end time, status MSGID1000;SAMPLE.TXT;01:05:55;07:08:44;OK MSGID1000;SAMPLE.TXT;01:05:55;07:08:44;NOK

I do this

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); 
  • 写回答

1条回答 默认 最新

  • dskvfdxgdo2422392 2016-06-04 07:42
    关注

    I found solution to extend LoggerLayoutPattern and override the getHeader() method.

    class csvLoggerLayoutPattern extends LoggerLayoutPattern {
        public function getHeader() {
            return "message id, file name, start time, end time, status
    ";
        }
    }
    
    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 为啥画版图在Run DRC会出现Connect Error?可我Calibre的hostname和计算机的hostname已经设置成一样的了。
  • ¥20 网站后台使用极速模式非常的卡
  • ¥20 Keil uVision5创建project没反应
  • ¥15 mmseqs内存报错
  • ¥15 vika文档如何与obsidian同步
  • ¥15 华为手机相册里面的照片能够替换成自己想要的照片吗?
  • ¥15 陆空双模式无人机飞控设置
  • ¥15 sentaurus lithography
  • ¥100 求抖音ck号 或者提ck教程
  • ¥15 关于#linux#的问题:子进程1等待子进程A、B退出后退出(语言-c语言)