原文链接:http://blog.csdn.net/johnnycode/article/details/44538895
1、支持不同日志级别输出到不同的文件
2、支持过滤第三方包
web.xml 配置信息,因为本文使用Spring,相关信息请自行参考!
<!-- 配置 Log4j -->
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>myWebApp.root</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.xml</param-value>
</context-param>
<!-- 开一条watchdog线程每60秒扫描一下配置文件的变化 -->
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>60000</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
log4j.xml 配置信息
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- STDOUT 级别配置 -->
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<param name="encoding" value="UTF-8" />
<param name="target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%-5p %d{HH:mm:ss}] %c - %m%n" />
</layout>
</appender>
<!-- ALL 级别配置 -->
<appender name="ALL" class="org.apache.log4j.DailyRollingFileAppender">
<param name="encoding" value="UTF-8" />
<param name="File" value="${myWebApp.root}/logs/all.log" />
<param name="Append" value="true" />
<param name="DatePattern" value="'.'yyyy-MM-dd'.log'" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%-5p %d{yyyy-MM-dd HH:mm:ss}] %c - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="DEBUG" />
<param name="LevelMax" value="ERROR" />
</filter>
</appender>
<!-- ERROR 级别配置 -->
<appender name="ERROR" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="${myWebApp.root}/logs/error.log" />
<param name="Append" value="true" />
<param name="MaxFileSize" value="50MB" />
<param name="MaxBackupIndex" value="1000" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%-5p %d{yyyy-MM-dd HH:mm:ss}] %c - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="ERROR" />
<param name="LevelMax" value="ERROR" />
</filter>
</appender>
<!-- category 定义第三方过滤条件 -->
<category name="org.springframework">
<priority value="warn" />
<appender-ref ref="ALL" />
</category>
<category name="org.hibernate">
<priority value="debug" />
<appender-ref ref="ALL" />
</category>
<root>
<appender-ref ref="STDOUT" />
<appender-ref ref="ALL" />
<appender-ref ref="ERROR" />
</root>
</log4j:configuration>