在web.xml文件中增加如下配置:
<?
xml version="1.0"
?>
<!--
* Copyright 2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
-->
<! DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
< web-app >
< servlet >
< servlet-name > log4j </ servlet-name >
< servlet-class > org.springframework.web.util.Log4jConfigServlet </ servlet-class >
< load-on-startup > 1 </ load-on-startup >
</ servlet >
< context-param >
< param-name > webAppRootKey </ param-name >
< param-value > BBS.root </ param-value >
</ context-param >
< context-param >
< param-name > log4jConfigLocation </ param-name >
< param-value > /WEB-INF/classes/log4j.properties </ param-value >
</ context-param >
< 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 >
</ web-app >
<!--
* Copyright 2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
-->
<! DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
< web-app >
< servlet >
< servlet-name > log4j </ servlet-name >
< servlet-class > org.springframework.web.util.Log4jConfigServlet </ servlet-class >
< load-on-startup > 1 </ load-on-startup >
</ servlet >
< context-param >
< param-name > webAppRootKey </ param-name >
< param-value > BBS.root </ param-value >
</ context-param >
< context-param >
< param-name > log4jConfigLocation </ param-name >
< param-value > /WEB-INF/classes/log4j.properties </ param-value >
</ context-param >
< 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 >
</ web-app >
log4j.properties配置(按照个人需要配置日志级别):
log4j.rootLogger=DEBUG,CONSOLE
log4j.addivity.org.apache=true
# ??????
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.Threshold=DEBUG
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
#log4j.appender.CONSOLE.layout.ConversionPattern=[start]%d{DATE}[DATE]%n%p[PRIORITY]%n%x[NDC]%n%t[THREAD] n%c[CATEGORY]%n%m[MESSAGE]%n%n
log4j.addivity.org.apache=true
# ??????
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.Threshold=DEBUG
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
#log4j.appender.CONSOLE.layout.ConversionPattern=[start]%d{DATE}[DATE]%n%p[PRIORITY]%n%x[NDC]%n%t[THREAD] n%c[CATEGORY]%n%m[MESSAGE]%n%n
之后将Log4j.jar加入到项目中,就可以在类中使用Log4j了:)
import
org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class TestLog4j ... {
private Log log=LogFactory.getLog(this.getClass());
public static void main(String[] args) ...{
if(log.isDebugEnabled())...{
log.debug("Test log4j");
}
}
}
import org.apache.commons.logging.LogFactory;
public class TestLog4j ... {
private Log log=LogFactory.getLog(this.getClass());
public static void main(String[] args) ...{
if(log.isDebugEnabled())...{
log.debug("Test log4j");
}
}
}