使用Log4j生成不同日志级别的日志文件

在src根目录下存放log4j.xml文件,具体配置如下(测试使用log4j-1.2.16.jar):

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

<!--
设置通道名称和输出方式,其中输出方式appender有5种,分别为:
org.apache.log4j.FileAppender (文件)
org.apache.log4j.ConsoleAppender (控制台)
org.apache.log4j.RollingFileAppender (根据文件大小创建新文件)
org.apache.log4j.DailyRollingFileAppender (每天产生一个日志文件)
org.apache.log4j.WriterAppender (将日志信息以流格式发送到指定地方)
-->
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
	<!-- 定义控制台日志级别入口  -->
	<param name="Threshold" value="DEBUG"/>
	<param name="Target" value="System.out" />
	<layout class="org.apache.log4j.PatternLayout">
		<param name="ConversionPattern" value="%d{yyyy-MM-dd hh:mm:ss,SSS} | %-5p | %-10t | %-24.24c{1} | %-30.30C %4L | %m%n"/>
	</layout>
</appender>

<appender name="DEBUG" class="org.apache.log4j.RollingFileAppender">
	<param name="File" value="c:/logs/log4j/debug.log"/>
	<param name="Append" value="true" />
	<param name="Threshold" value="DEBUG"/>
	<param name="MaxFileSize" value="10MB" />
	<param name="MaxBackupIndex" value="30" />
	<layout class="org.apache.log4j.PatternLayout">
		<param name="ConversionPattern" value="%d{yyyy-MM-dd hh:mm:ss,SSS} | %-5p | %-10t | %-24.24c{1} | %-30.30C %4L | %m%n"/>
	</layout>
	<filter class="org.apache.log4j.varia.LevelRangeFilter">
		<param name="LevelMin" value="DEBUG"/>
		<param name="LevelMax" value="DEBUG"/>
	</filter>
</appender>

<!-- 每天生成一个文件,注意由于window不支持特殊字符命名,在配置DatePattern是不能用":"-->
<appender name="INFO" class="org.apache.log4j.DailyRollingFileAppender">
	<param name="Threshold" value="INFO"/>
	<param name="File" value="c:/logs/log4j/info.log"/>
	<param name="DatePattern" value="'.'yyyy-MM-dd'.log'"/>
	<layout class="org.apache.log4j.PatternLayout">
		<param name="ConversionPattern" value="%d{yyyy-MM-dd hh:mm:ss,SSS} | %-5p | %-10t | %-24.24c{1} | %-30.30C %4L | %m%n"/>
	</layout>
	<!-- 过滤器只记录info级别的日志,以下同  -->
	<filter class="org.apache.log4j.varia.LevelRangeFilter">
		<param name="LevelMin" value="INFO"/>
		<param name="LevelMax" value="INFO"/>
	</filter>
</appender>

<!-- warn -->
<appender name="WARN" class="org.apache.log4j.FileAppender">
	<param name="Threshold" value="WARN"/>
	<param name="File" value="c:/logs/log4j/warn.log"/>
	<layout class="org.apache.log4j.PatternLayout">
		<param name="ConversionPattern" value="%d{yyyy-MM-dd hh:mm:ss,SSS} | %-5p | %-10t | %-24.24c{1} | %-30.30C %4L | %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="ERROR" class="org.apache.log4j.FileAppender">
	<param name="Threshold" value="ERROR"/>
	<param name="File" value="c:/logs/log4j/error.log"/>
	<layout class="org.apache.log4j.PatternLayout">
		<param name="ConversionPattern" value="%d{yyyy-MM-dd hh:mm:ss,SSS} | %-5p | %-10t | %-24.24c{1} | %-30.30C %4L | %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="FATAL" class="org.apache.log4j.FileAppender">
	<param name="Threshold" value="FATAL"/>
	<param name="File" value="c:/logs/log4j/fatal.log"/>
	<layout class="org.apache.log4j.PatternLayout">
		<param name="ConversionPattern" value="%d{yyyy-MM-dd hh:mm:ss,SSS} | %-5p | %-10t | %-24.24c{1} | %-30.30C %4L | %m%n"/>
	</layout>
	<filter class="org.apache.log4j.varia.LevelRangeFilter">
		<param name="LevelMin" value="FATAL"/>
		<param name="LevelMax" value="FATAL"/>
	</filter>
</appender>

<logger name="org.apache.commons.dbcp">
	<level value="INFO"/>
</logger>
<logger name="org.hibernate.cache">
	<level value="DEBUG"/>
</logger>
<logger name="org.hibernate">
	<level value="WARN"/>
</logger>
<logger name="org.apache">
	<level value="WARN"/>
</logger>
<logger name="org.springframework">
	<level value="WARN"/>
</logger>
<logger name="org.jencks">
	<level value="WARN"/>
</logger>
<logger name="org.apache.activemq">
	<level value="WARN"/>
</logger>
<logger name="org.apache.activemq.transport.discovery">
	<level value="ERROR"/>
</logger>
<logger name="org.apache.servicemix">
<!-- To enable debug logging, replace the INFO by DEBUG -->
	<level value="WARN"/>
</logger>
<logger name="org.apache.servicemix.jbi.config">
	<level value="WARN"/>
</logger>
<logger name="org.apache.servicemix.jbi.deployment">
	<level value="WARN"/>
</logger>
<logger name="org.directwebremoting">
	<level value="WARN"/>
</logger>

<root>
	<level value="DEBUG"/><!-- 定义日志级别 -->
	<appender-ref ref="CONSOLE"/>
	<appender-ref ref="DEBUG"/>
	<appender-ref ref="INFO"/>
	<appender-ref ref="WARN"/>
	<appender-ref ref="ERROR"/>
	<appender-ref ref="FATAL"/>
</root>
</log4j:configuration>

 在开发过程中会提示找不到"log4j.dtd",解压log4j.jar文件,将org.apache.log4j.xml中的log4j.dtd复制到src根目录即可.

 

你可能感兴趣的:(log4j)