2013-04-05 147 views
2

我有這個log4j.config配置log4j文件

<appender name="FileInfoStatistics" class="org.apache.log4j.RollingFileAppender"> 
<param name="file" value="${log.dir}/T_Statistics.log" /> 
<param name="MaxFileSize" value="8000KB"/> 
<!-- Keep one backup file --> 
<param name="MaxBackupIndex" value="1"/> 
<layout class="org.apache.log4j.PatternLayout"> 
    <param name="ConversionPattern" value="%d %5p %c{1} - %m%n" /> 
</layout> 
<filter class="org.apache.log4j.varia.LevelRangeFilter"> 
    <param name="levelMin" value="INFO" /> 
</filter> 
</appender> 

但我想就當接檔T_Statistics.log創建firt時間和一個行如該日誌文件,這是可能的log4j的距離。配置?

我看到這是有可能的連接log4net的有:

<param name="Header" value="xxxx" 

有什麼樣的log4j的?

回答

2

那麼,經過一番研究,我找到了解決我的問題,說如果有人會有用。

我創建了一個自定義的appender:

package com.mycompany.logger; 

import org.apache.log4j.PatternLayout; 

public class MyPatterLayout extends PatternLayout 
{ 
    @Override 
    public String getHeader() 
    { 
     return "message id, file name, start time, end time, status" + System.getProperty("line.separator"); 
    } 
} 

然後,我已經配置了我的log4j.xml:

<appender name="console" class="org.apache.log4j.ConsoleAppender"> 
<param name="Target" value="System.out"/> 
<layout class="com.mycompany.logger.MyPatterLayout"> 
    <param name="ConversionPattern" value="%-5p %c{1} - %m%n"/> 
</layout> 

這是結果:

message id, file name, start time, end time, status 
DEBUG App - Here is some DEBUG 
INFO App - Here is some INFO 
WARN App - Here is some WARN 
ERROR App - Here is some ERROR 
FATAL App - Here is some FATAL