[Tip] Log4j的配置

[Tip] Log4j的配置

 

关于log4j.properties文件的位置:

配置文件log4j.properties的默认路径是在src下,eclipse编译的时候,会把它拷贝到classes目录下。

可以在web.xml中设置log4j的路径,不过,相应的,需要在servletinit()方法中对配置文件进行处理。

 

示例如下:

web.xml文件:

<servlet>

<servlet-name>log4j-init</servlet-name>

<servlet-class>chapter3.Log4jInitServlet</servlet-class>

<init-param>

<param-name>log4j-init-file</param-name>

<param-value>WEB-INF/classes/log4j.properties</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

 

servlet中的init()方法:

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import java.io.PrintWriter;

import java.io.IOException;

public class Log4jInitServlet extends HttpServlet {

public void init() {

String prefix = getServletContext().getRealPath("/");

String file = getInitParameter("log4j-init-file");

// if the log4j-init-file is not set, then no point in trying

if(file != null) {

PropertyConfigurator.configure(prefix+file);

}

}

public void doGet(HttpServletRequest req, HttpServletResponse res) {

}

}

注:代码摘自 log4j – The Complete Manual  by Ceki Gülcü

 

如果使用的是struts的话,有2种解决方法:

一.  继承ActionServlet类,重写init()方法,然后在web.xml中使用这个子类。

二.  我在 struts in action 中看到另外一种方法,摘录如下:

9.4 PlugIn

Many applications need access to specialized resources. These may be data access components, Properties files, or any number of business-tier components. Many of the developer extensions to Struts, such as menu systems, alternate validators, data transformers, XLS converters, and so forth, need to parse their own configuration

files and create their own controller objects.

In a conventional Java web application, these objects would usually be initialized as part of the servlet’s init method and released as part of its destroy method. We could subclass the ActionServlet and do the same thing, but that can lead to contention when a developer wants to use more than one third-party Struts extension.

Since Actions are already the servlet’s delegate in most matters, the framework offers a PlugIn interface (org.apache.stuts.action.PlugIn) that allows an Action to implement init and destroy methods, like those on a servlet. Since the Action has access to the servlet as a member variable, any resource that could be initialized

or released by a servlet can be handled through a PlugIn Action.

The PlugIn is registered with the controller through the Struts configuration using an element like this:

 

<plug-in className="myApp.MyAction>

<set-property property="key" value="MY_APP_KEY"/>

</plug-in>

 

The Action specified by the className attribute must implement the PlugIn interface and provide an init and a destroy method. If the Action has any member variables that need to be initialized, the standard <set-property> elements can be

used to pass a value to any arbitrary property.

On startup, the controller initializes the PlugIn Actions, sets any properties, and calls the init method. On shutdown, the controller calls the destroy method on each PlugIn Action before releasing it.

 

关于输出的log文件的位置:

log输出如果采用输出到log文件中的方式,默认情况下,log文件会位于Tomcatbin目录下。

如:log4j.appender.LOGFILE.File=shitingguanai.log

就会在bin目录下生成一个名为shitingguanailog

 

=============================>20071220 update begins<============================

在Tomcat5.5中,默认的相对路径目录为Tomcat的根目录, 即%CATALINA_HOME%

=============================>20071220 update ends<============================

指定log的位置可以使用相对路径。

如:log4j.appender.LOGFILE.File=../logs/shitingguanai.log

log文件就会位于Tomcatlog目录下。

 

同样可以把log指定到某个应用下:

如:log4j.appender.LOGFILE.File=../webapps/myapp/log/shitingguanai.log

log文件位于一个叫做myapp的应用下的log目录中。

 

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值