---
title: "Web工程部署到WebLogic9.2配置Log4j问题"
author: "hareee"
date: "2011-05-18T13:49:25Z"
tags: []
categories: ["企业架构"]
slug: "web-weblogic9-2-log4j"
draft: false
lastmod: "2011-05-18T13:53:35Z"
id: 1048349
url: "https://hareee.iteye.com/blog/1048349"
---

使用commons-logging接口管理log4j打印日志

 

现象：

1.开发中使用tomcat作应用服务器，只要吧log4j.properties放到src下即可正确的打印出日志。

2.开发中使用WebLogic9.2作为应用服务器

a)发布成Exploded Archive，打印日志正常

b)发布成Packaged Archive，不能打印日志

怀疑打包后WebLogic不能读取log4j配置文件

 

原因：

在web.xml中log4j的配置方式问题，默认情况log4j.properties放在classes目录下，而由于war没展开，找不到这个文件，所以把它直接放WEB-INF目录下，并去掉原来web.xml里面的如下配置：

 

```
        <servlet>
                  <servlet-name>log4j</servlet-name>
                  <servlet-class>
                           org.springframework.web.util.Log4jConfigServlet
                  </servlet-class>
                  <load-on-startup>1</load-on-startup>
        </servlet>
```

 

 

解决办法：

新建一个servlet，初始化log4j配置文件：

 

```
public void init() throws ServletException {
                  InputStream is = getServletContext().getResourceAsStream(
                                    "/WEB-INF/classes/log4j.properties");
                  Properties props = new Properties();
                  try {
                           props.load(is);
                  } catch (IOException e) {
                           System.err.println("Load log4j configuration failed");
                  }
                  PropertyConfigurator.configure(props);
        }

```

 

 

 并注册到web.xml中