首先配置好log4j的配置文件,在/WEB-INF目录下建立log4j.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="%d{ABSOLUTE} %5p %c{1}:%L - %m%n"/>
</layout>
</appender>
<!-- 以下根据级别将日志信息输出到不同的文件中-->
<!--设定级别为debug的配置信息-->
<appender name="DEBUG" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="C://logs//app-debug.log"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="500MB"/>
<param name="MaxBackupIndex" value="2"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} %5p %c{1}:%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="INFO" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="C://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="%d{ABSOLUTE} %5p %c{1}:%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="WARN" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="C://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="%d{ABSOLUTE} %5p %c{1}:%L - %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="WARN" />
<param name="LevelMax" value="WARN" />
</filter>
</appender>
<!--设定级别为deb的配置信息-->
<appender name="ERROR" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="d://logs//error.log"/>
<param name="Append" value="true"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} %5p %c{1}:%L - %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="ERROR" />
<param name="LevelMax" value="ERROR" />
</filter>
</appender>
<!--配置具体要实现的方式,这边只设定了STDOUT,ERROR这2种情况,没有使用到的情况,不要在上面
配置,否则会出错
-->
<root>
<priority value="info"/>
<appender-ref ref="STDOUT"/>
<appender-ref ref="ERROR"/>
</root>
</log4j:configuration>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
这边指定了log4j.dtd,记得把该用户也拷贝到/WEB-INF目录下
接下来在web.xml中,利用spring将log4j配置为listener,如下所示:
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.xml</param-value>
</context-param