Log4j使用--简单配置

本文介绍如何在web.xml中配置Log4j,并提供log4j.properties文件的示例配置,以便实现不同级别的日志记录。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在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启动的时候会在这个目录搜索这个文件,如果没有发现这个文件就会报错。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值