weblogic下war包发布的项目中log4j的使用

 网上可以找到的log4j的嵌入方式有2种:

1、将log4j.properties放在classpath中,一般是web-inf/classes下,缺点的缺乏灵活性

2、可以通过log4j的com.apache.jakarta.log4j.Log4jInit类,Spring的 org.springframework.web.util.Log4jConfigServlet和 org.springframework.web.util.ServletContextListener来进行嵌入。

 

如果项目是以war包形式发布到weblogic中时方法2将会遇到问题。war包在weblogic中是不会自动展开的,这一点与tommcat不一样,换句话说,你就不能通过绝对路径来得到资源,而要以另一种形式ServletContext.getResourceAsStream()来进行读取。方法2中说到的几个类都是先取到文件的绝对路径然后再来读取,在weblogic启动的时候会出现错误

Cannot set web app root system property when WAR file is not expanded。

那么只好自己来进行初始化了


下面的就是新鲜出炉的log4j初始化类和web.xml文件中的配置

 

  1. package util;
  2. import java.io.IOException;
  3. import java.util.Properties;
  4. import javax.servlet.http.HttpServlet;
  5. import org.apache.log4j.PropertyConfigurator;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. public class Log4jInit extends HttpServlet {
  9.     public void init() {
  10.         /**
  11.          * apache中的调用方法
  12.          * String prefix =getServletContext().getRealPath("/");
  13.          * 
  14.          * String file = getInitParameter("log4j");
  15.          *  // if the log4j-init-file is not set, then no point in trying
  16.          * 
  17.          * System.out.println("................log4j start");
  18.          * 
  19.          * if(file != null) {
  20.          * 
  21.          * PropertyConfigurator.configure(prefix+file);
  22.          *  }
  23.          * 
  24.          */
  25.         String file = getInitParameter("log4j");        
  26.         System.out.println("................log4j start");
  27.         if (file != null) {
  28.             Properties ps=new Properties();
  29.             try {
  30.                 ps.load(getServletContext().getResourceAsStream(file));
  31.             } catch (IOException e) {
  32.                 e.printStackTrace();
  33.             }
  34.             PropertyConfigurator.configure(ps);
  35.         }
  1.     <servlet>
  2.         <servlet-name>log4jLoader</servlet-name>
  3.         <servlet-class>util.Log4jInit</servlet-class>
  4.         <init-param>
  5.             <param-name>log4j</param-name>
  6.             <param-value>/WEB-INF/log4j.properties</param-value>
  7.         </init-param>
  8.         <load-on-startup>0</load-on-startup>
  9.     </servlet>

下面的话摘自网上:

那么,现在对于war应用可以成功运行,但如果现在不通过war部署,直接通过目录结构部署应用会不会又出现找不到资源的错误呢?请来看看ServletContext.getResourceAsStream的API文档,

Returns a URL to the resource that is mapped to a specified path. The path must begin with a "/" and is interpreted as relative to the current context root. 
This method allows the servlet container to make a resource available to servlets from any source. Resources can be located on a local or remote file system, in a database, or in a .war file. 

可见,通过getResourceAsStream可以获取包括本地文件系统、远程文件系统、war包等资源。不会出现上面担心的问题。

结论:在开发J2EE Web应用时,如果需要读取本应用中的文件,尽量使用ServletContext.getResourceAsStream进行,而不要使用文件IO。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值