<!-- Tomcat使用 --> <!-- <context-param> <param-name>log4jConfigLocation</param-name> <param-value> classpath:config/log4j.xml </param-value> </context-param> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:config/applicationContext.xml </param-value> </context-param> --> <!-- JBoss使用 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:config/log4j.xml classpath:config/applicationContext.xml </param-value> </context-param>主要分为两个部分,分别在Tomcat和JBoss下使用,但需要注意使用contextConfigLocation时,不支持log4j的properties配置文件,只可以使用xml。
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE log4j:configuration SYSTEM "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd"> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> <!-- 控制台配置 --> <appender name="console" class="org.apache.log4j.ConsoleAppender"> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" /> </layout> </appender> <!-- 文件配置 --> <!-- <appender name="file" class="org.apache.log4j.RollingFileAppender"> <param name="append" value="false" /> <param name="maxFileSize" value="10MB" /> <param name="maxBackupIndex" value="10" /> <param name="file" value="${catalina.home}/logs/pro.log" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" /> </layout> </appender> --> <root> <level value="WARN" /> <appender-ref ref="console" /> <!-- <appender-ref ref="file" /> --> </root> </log4j:configuration>其中需要注意下,使用网上经常能搜到的<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">是会报错的。
原文地址:http://blog.csdn.net/tylanbin/article/details/39995841