log4j 配置文件 (XML/.properties)

xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>

<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%c %d{ISO8601}
-- %p -- %m%n"/>
</layout>
</appender>

<appender name="DEBUG" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="logs\\app-debug.log"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="500KB"/>
<param name="MaxBackupIndex" value="2"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%c %d{ISO8601}
-- %p -- %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="DEBUG" />
<param name="LevelMax" value="DEBUG" />
</filter>
</appender>

<appender name="INFO" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="logs\\app-info.log"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="500KB"/>
<param name="MaxBackupIndex" value="2"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%c %d{ISO8601}
-- %p -- %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="INFO" />
<param name="LevelMax" value="INFO" />
</filter>
</appender>

<appender name="WARN" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="logs\\app-warn.log"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="500KB"/>
<param name="MaxBackupIndex" value="2"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%c %d{ISO8601}
-- %p -- %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="WARN" />
<param name="LevelMax" value="WARN" />
</filter>
</appender>

<appender name="ERROR" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="logs\\app-error.log"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="500KB"/>
<param name="MaxBackupIndex" value="2"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%c %d{ISO8601}
-- %p -- %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="ERROR" />
<param name="LevelMax" value="ERROR" />
</filter>
</appender>

<root>
<priority value="debug"/>
<appender-ref ref="STDOUT"/>
<appender-ref ref="DEBUG"/>
<appender-ref ref="INFO"/>
<appender-ref ref="WARN"/>
<appender-ref ref="ERROR"/>
</root>

</log4j:configuration>

 log4j.properties:

#日志输出级别为info,根日志器输出目的地为stdout,file 
log4j.rootLogger=info, stdout, file 

#stdout为控制台 

log4j.appender.stdout=org.apache.log4j.ConsoleAppender 
#指定向stdout控制台输出的日志的格式  log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
#%m 输出代码中指定的消息 
#%p 输出优先级,即DEBUG,INFO,WARN,ERROR,FATAL 
#%r 输出自应用启动到输出该log信息耗费的毫秒数 
#%c 输出所属的类目,通常就是所在类的全名 
#%t 输出产生该日志事件的线程名 
#%n 输出一个回车换行符,Windows平台为“rn”,Unix平台为“n” 
#%d 输出日志时间点的日期或时间,默认格式为ISO8601,也可以在其后指定格式,比如:%d{yyy MMM dd HH:mm:ss,SSS},输出类似:20101231日 201029921  #%l 输出日志事件的发生位置,包括类目名、发生的线程,以及在代码中的行数。 

log4j.appender.stdout.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
#log4j.appender.stdout.layout.ConversionPattern=[start]%d{yyyy/MM/dd/ HH:mm:ss}[DATE]%n%p[PRIORITY]%n%x[NDC]%n%t[THREAD] n%c[CATEGORY]%n%m[MESSAGE]%n%n

#file日志文件 
log4j.appender.file=org.apache.log4j.FileAppender                           
log4j.appender.file.File=log/system.log            
#定向file控制台输出的日志的格式 //log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d [%t] %-5p %c - %m%n #log4j.appender.file.layout.ConversionPattern=[start]%d{yyyy/MM/dd/ HH:mm:ss}[DATE]%n%p[PRIORITY]%n%x[NDC]%n%t[THREAD] n%c[CATEGORY]%n%m[MESSAGE]%n%n log4j.logger.org.apache.commons=ERROR log4j.logger.com.opensymphony.oscache.base=INFO #hibernate日志文件
#记录所有的信息
log4j.logger.org.hibernate=info
#记录JDBC参数  log4j.logger.org.hibernate.type=debug log4j.logger.org.hibernate.hql.ast.AST=debug #log4j.logger.org.hibernate=debug ### log HQL query parser activity #log4j.logger.org.hibernate.hql.ast.AST=debug
#记录执行的SQL DML语句  log4j.logger.org.hibernate.SQL=debug ### log JDBC bind parameters ### log4j.logger.org.hibernate.type=info #log4j.logger.org.hibernate.type=debug
#记录执行的SQL DDL语句 ### log4j.logger.org.hibernate.tool.hbm2ddl=debug ### log HQL parse trees #log4j.logger.org.hibernate.hql=debug ### 记录第二级缓存的活动 ### #log4j.logger.org.hibernate.cache=debug ### 记录与事务有关的活动  #log4j.logger.org.hibernate.transaction=debug ###记录得到的JDBC资源 #log4j.logger.org.hibernate.jdbc=debug ### enable the following line if you want to track down connection ### ### leakages when using DriverManagerConnectionProvider ### log4j.logger.org.hibernate.connection.DriverManagerConnectionProvider=trace #for spring #log4j.rootLogger=DEBUG,Spring log4j.appender.Spring=org.apache.log4j.ConsoleAppender #log4j.appender.Spring.Target=System.out log4j.appender.Spring.layout=org.apache.log4j.PatternLayout log4j.appender.Spring.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

测试代码:

package com.nsrl.test;

import java.io.IOException;

import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;

public class testlog4j {
public static void main(String[] args) throws IOException, ClassNotFoundException {
    DOMConfigurator.configure("src/log4j2.xml");
       Logger log=Logger.getLogger("com.nsrl.dante");
       log.info("配置成功"); 

 

转载请注明文章出处:http://www.cnblogs.com/gredswsh   Gredswsh  专栏

posted @ 2014-06-02 16:44  fxcl  阅读(1268)  评论(0编辑  收藏  举报