2

我有这个 log4j.config

<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 时创建一行,例如这是一个日志文件,这可能来自 log4j.config 吗?

我看到这在 log4net 中是可能的:

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

log4j中有类似的东西吗?

4

1 回答 1

2

Well, After some research I found a solution to my problem, said if someone would be useful.

I have created a custom 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");
    }
}  

And then I have configured my 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>

And this is the result:

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
于 2013-04-07T08:41:47.673 回答