Log4j使用--简单配置

来源:互联网 发布:北京seo学校哪家好 编辑:程序博客网 时间:2024/05/16 06:35

在web.xml文件中增加如下配置:

<?xml version="1.0"?>

<!--
 * Copyright 2004 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
 * 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 &quot;AS IS&quot; 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.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");
  }


}


}


注意点:log4j.properties系统默认的路径是在WEB-INF/classes/下的,Weblogic启动的时候会在这个目录搜索这个文件,如果没有发现这个文件就会报错。
原创粉丝点击