log4j

log4j的属性文件配置:

可以在web系统中在web.xml中配置上下文参数定义配置文件路径,例如:


<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param>


也可以用java代码来动态改变,例如:

PropertyConfigurator.configure ( String configFilename) ;//读取使用Java的特性文件编写的配置文件。
DOMConfigurator.configure ( String filename );//读取XML形式的配置文件。



进一步,在log4j中获得应用application web app的路径:

<context-param>
<param-name>webAppRootKey</param-name>
<param-value>countries.root</param-value>
</context-param>


如果不定义webAppRootKey参数,那么webAppRootKey就是缺省的"webapp.root"。在log4j的配置文件中来引用:

log4j.appender.logfile.File=${countries.root}/WEB-INF/countries.log

还有其他的一些配置:

<!--Spring默认刷新Log4j配置文件的间隔,单位为millisecond-->
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>60000</param-value>
</context-param>

<!-- Web 项目 Spring 加载 Log4j 的监听 -->
<!-- Leave the listener commented-out if using JBoss (这个是spring的配置文件中说明的)-->
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-clas>
</listener>


[quote]
这个Log4jInitServlet主要方便之处是设置了系统变量webapp.root,在log4j.properties文件中就可以这样指定log文件路径。使用${webapp.root}表示web应用的根目录。全小写字母。
log4j.appender.FILE.File=${webapp.root}/WEB-INF/testlog4j.log

参考Spring对log4j的增强,增加webAppRootKey参数,防止多web应用,系统变量重复。


package com.suncode.log4j;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.PropertyConfigurator;

/**
* A servlet for initializing Log4j. See <a href=
* "http://jakarta.apache.org/log4j/docs/documentation.html">Log4j documentation</a>
* for how to use Log4j.
* <p>
* can use ${webapp.root} as your web app root in your log4j.property file as
* follows: log4j.appender.FILE.File=${webapp.root}/WEB-INF/testlog4j.log
*
* This servlet is never called by a client, but should be called during web
* application initialization, i.e. when the servlet engine starts. The
* following code should be inserted in the web.xml file for the web
* application:
* </p>
* <servlet> <servlet-name>Log4jInit</servlet-name>
* <servlet-class>com.suncode.log4j.Log4jInitServlet</servlet-class>
* <load-on-startup>1</load-on-startup>
* <init-param>
* <param-name>log4j-configuration</param-name>
* <param-value>/WEB-INF/log4j.properties</param-value>
* </init-param>
* <!--如果有多个web应用,怕webapp.root变量重复,可以在init-param里定义webAppRootKey-->
* <init-param>
* <param-name>webAppRootKey</param-name>
* <param-value>webapp.root</param-value>
* </init-param>
* </servlet>
*
* 2006-1-9 增加webAppRootKey参数,防止多web应用,系统变量重复.
*/
public class Log4jInitServlet extends HttpServlet {

private final String WEB_APP_ROOT_DEFAULT = "webapp.root";

public void init() throws ServletException {
String prefix = getServletContext().getRealPath("/");
// System.setProperty("webapp.root", prefix);
String webAppRootKey = getServletConfig().getInitParameter(
"webAppRootKey");
if(webAppRootKey == null || webAppRootKey.length() < 1){
webAppRootKey = WEB_APP_ROOT_DEFAULT;
}

System.setProperty(webAppRootKey, prefix);

String propfile = getServletConfig().getInitParameter(
"log4j-config-file");
if (propfile != null) {
PropertyConfigurator.configure(prefix + propfile);
System.out.println("Log4J Configured.");
}
}

protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Method stub generated by Lomboz
}

protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Method stub generated by Lomboz
}
}

[/quote]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值