log4j配置文件,xml格式,分级输出

一、配置文件xmllog4jconfig.xml如下,放到工程的根目录

<?xml version="1.0" encoding="GB2312" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<!--#log4j中有5级logger ,#FATAL 0 ,#ERROR 3 ,#WARN 4 ,#INFO 6 ,#DEBUG 7 -->
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>

<!--输出到控制台-->
<!--
<appender name="LOG.Console" class="org.apache.log4j.ConsoleAppender">  
<layout class="org.apache.log4j.PatternLayout">  
  <param name="ConversionPattern" value="%d{yyy-MM-dd HH:mm:ss} [%-5p] %c {%F:%L} - %m%n" />  
</layout>  
</appender>
-->

<!--将级别为DEBUG的信息输出到控制台-->
<appender name="LOG.DEBUG" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="d:/log/debug.log" />
<param name="MaxFileSize" value="5120KB" />
<param name="MaxBackupIndex" value="10" />
<layout class="org.apache.log4j.PatternLayout">
  <param name="ConversionPattern" value="%d{yyy-MM-dd HH:mm:ss} [%-5p] %c {%F:%L} - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">  
  <param name="LevelMin" value="DEBUG" />  
  <param name="LevelMax" value="DEBUG" />  
</filter>
</appender>

<!--将级别为INFO的信息输出到控制台-->
<appender name="LOG.INFO" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="d:/log/info.log" />
<param name="MaxFileSize" value="5120KB" />
<param name="MaxBackupIndex" value="10" />
<layout class="org.apache.log4j.PatternLayout">
  <param name="ConversionPattern" value="%d{yyy-MM-dd HH:mm:ss} [%-5p] %c {%F:%L} - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">  
  <param name="LevelMin" value="INFO" />  
  <param name="LevelMax" value="INFO" />  
</filter>
</appender>

<!--将级别为WARN的信息输出到控制台-->
<appender name="LOG.WARN" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="d:/log/warn.log" />
<param name="MaxFileSize" value="5120KB" />
<param name="MaxBackupIndex" value="10" />
<layout class="org.apache.log4j.PatternLayout">
  <param name="ConversionPattern" value="%d{yyy-MM-dd HH:mm:ss} [%-5p] %c {%F:%L} - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">  
  <param name="LevelMin" value="WARN" />  
  <param name="LevelMax" value="WARN" />  
</filter>
</appender>

<!--将级别为ERROR的信息输出到控制台-->
<appender name="LOG.ERROR" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="d:/log/error.log" />
<param name="MaxFileSize" value="5120KB" />
<param name="MaxBackupIndex" value="10" />
<layout class="org.apache.log4j.PatternLayout">
  <param name="ConversionPattern" value="%d{yyy-MM-dd HH:mm:ss} [%-5p] %c {%F:%L} - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">  
  <param name="LevelMin" value="ERROR" />  
  <param name="LevelMax" value="ERROR" />  
</filter>
</appender>

<!--将级别为FATAL的信息输出到控制台-->
<appender name="LOG.FATAL" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="d:/log/fatal.log" />
<param name="MaxFileSize" value="5120KB" />
<param name="MaxBackupIndex" value="10" />
<layout class="org.apache.log4j.PatternLayout">
  <param name="ConversionPattern" value="%d{yyy-MM-dd HH:mm:ss} [%-5p] %c {%F:%L} - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">  
  <param name="LevelMin" value="FATAL" />  
  <param name="LevelMax" value="FATAL" />  
</filter>
</appender>

<!--
<appender name="InitAction" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="d:/dbcon.log"/>
<param name="MaxFileSize" value="5120KB"/>
<param name="MaxFileSize" value="10" />
<param name="MaxBackupIndex" value="2" />
<param name="DatePattern" value="'.'yyyy-MM-dd'.'log"/>
<layout class="org.apache.log4j.PatternLayout">
  <param name="ConversionPattern" value="%d{yyy-MM-dd HH:mm:ss} - %m%n"/>
  <param name="ConversionPattern" value="%d{DATE} [%-5p] %c {%F:%L} - %m%n"/>
</layout>
</appender>
-->

<!--设置org.logicalcobwebs包的输出级别为INFO-->
<!--
<category name="org.logicalcobwebs">
<priority value="INFO" />
<appender-ref ref="LOG.Console" />
</category>
-->
<root>
<priority value="DEBUG"/>
<!--
<appender-ref ref="LOG.Console" />
  -->
<appender-ref ref="LOG.DEBUG" />
<appender-ref ref="LOG.INFO" />
<appender-ref ref="LOG.WARN" />
<appender-ref ref="LOG.ERROR" />
<appender-ref ref="LOG.FATAL" />
</root>

</log4j:configuration>

 
二、新建Simpandfile.java如下

 

import junit.framework.Test;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.xml.DOMConfigurator;
public class Simpandfile
{
static Logger logger = Logger.getLogger(Test.class);

public static void main(String args[])
{
// 以property方式读到配置文件。
for(int i=0;i<100;i++)
{
//PropertyConfigurator.configure("simple.properties");
DOMConfigurator.configure("xmllog4jconfig.xml");
logger.debug("Here is some DEBUG");
logger.info("Here is some INFO");
logger.warn("Here is some WARN");
logger.error("Here is some ERROR");
logger.fatal("Here is some FATAL");
}
}
} 
 

 

你可能感兴趣的:(apache,C++,c,log4j,xml)