CSDN博客

img baichuan_

log4j学习(下)

发表于2004/9/17 19:02:00  1240人阅读

log4j学习(下)

上节中小结了一下在普通的java应用程序中如何使用commons-logging 和log4j 包,这一节将小结在web应用程序中如何使用上述两个包

 

1)将commongs-logging.jar 和 log4j-1.2.8.jar 包拷贝一份到web应用 的lib目录中,如下图:

hello

 --Module directory

   --WEB-INF

--classes

  --com

--lib

  --commons-logging.jar

  --log4j-1.2.8.jar

   --testlog4j.jsp

 

2) 配置log4j.xml 文件,放置在classes目录下(当然也可以放在任意目录下),log4j.xml内容如下:

log4j.xml

 

<?xml version="1.0" encoding="UTF-8">
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
 
<log4j:configuration xmlns:log4j="http://jakarata.apache.org/log4j/">
 
<appender name="file" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="log.log"/>
<layout class="org.apache.log4j.PatternLayout">
 <param name="ConVersionPattern" value="%d [%t] %p - %m%n"/>
</layout>
</appender>
 
<logger name="com.log4j.test.test" additivity="false">
<level value="debug"/>
<appender-ref ref="file"/>
</logger>
 
<root>
<level value="info"/>
<appender-ref ref="file"/>
</root>
 
</log4j:configuration>

注: log4j.xml配置参数含义请参见log4j学习(上)

 

3)写一个读取log4j.xml文件的servlet 类,名叫InitServlet:

package com.log4j.test.test;

 

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

 

import org.apache.log4j.xml.DOMConfigurator;

import org.apache.log4j.LogManager;

 

public class InitServlet

    extends HttpServlet {

 

  public void init() throws ServletException {

 

     String logFileUrl = "classes/config/log4j.xml";

     int reFreshTime = 60; //by secondes

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

 

    if (getInitParameter("LOG4J_FILE_URL") != null &&

        getInitParameter("LOG4J_FILE_URL").length() > 0) {

      logFileUrl = getInitParameter("LOG4J_FILE_URL");

    }

    if (getInitParameter("REFLESH_TIME") != null &&

        getInitParameter("REFLESH_TIME").length() > 0) {

      try

      {

        reFreshTime = Integer.parseInt(getInitParameter("REFLESH_TIME"));

      }catch(NumberFormatException ex)

      {

        ex.printStackTrace();

      }

 

    }

 

    //日志初始化

    print("log4j config url :" + prefix+logFileUrl + " reflesh time is " +reFreshTime );

    try {

      DOMConfigurator.configureAndWatch(prefix+logFileUrl, reFreshTime * 1000);

    }

    catch (Exception ex) {

      ex.printStackTrace();

      print("log4j config failure !");

    }

    print("log4j config success !");

 

  }

 

  public boolean init(HttpServlet hs) {

    return true;

  }

 

  public void destory() {

    LogManager.shutdown();

    print("log4j shutdown");

  }

  private void print(String msg)

  {

    System.out.println("[in com.log4j.test.test.InitServlet.java]" + msg);

  }

 

}

 

4) 在web.xml声明该servlet ,从而让该web一发布就自动调用上面servlet配置log4j的环境变量。

web.xml :

 <?xml version="1.0" encoding="UTF-8"?>
<!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>
  <display-name>hello</display-name>
  <servlet>
  <!-- LOG4J 初始化 servlet -->
  <servlet-name>LogInit</servlet-name>
  <servlet-class>com.log4j.test.test.InitServlet</servlet-class>
  <init-param>
  <!-- log4j config file url -->
  <param-name>LOG4J_FILE_URL</param-name>
  <param-value>/WEB-INF/classes/log4j.xml</param-value>
  </init-param>
  <init-param>
  <!-- reflesh time by  seconds  -->
  <param-name>REFLESH_TIME</param-name>
  <param-value>5</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
  </servlet>
</web-app>

 

 

5) ok ,经过上面的4步,log4j基本就配置好了,当该web应用一发布的时候,后台就会打印如下信息:

[in com.log4j.test.test.InitServlet.java]log4j config url :D:/program/log/hello/
WEB-INF/classes/log4j.xml reflesh time is 5
[in com.log4j.test.test.InitServlet.java]log4j config success !

6)  写一个测试的jsp页面:

testlog4j.jsp :

<%@ page contentType="text/html; charset=GBK" %>

<%@ page import="org.apache.commons.logging.*"%>

<%@ page import="com.log4j.test.TestLog4j2"%>

<%!

 private  Log log = LogFactory.getLog("testlog4j.jsp");

%>

<%

log.error("jsp log4j error");

log.warn("jsp log4j warn");

log.info("jsp log4j info");

log.debug("jsp log4j debug");

%>

This is Log4j Test !

<%=("helllo")%>

 

 

运行后后台打印信息:

testlog4j.jsp jsp_servlet.__testlog4j._jspService(__testlog4j.java:139) 2004-09-

20 17:09:23,014 [ExecuteThread: '14' for queue: 'weblogic.kernel.Default'] ERROR

 - jsp log4j error

testlog4j.jsp jsp_servlet.__testlog4j._jspService(__testlog4j.java:140) 2004-09-

20 17:09:23,014 [ExecuteThread: '14' for queue: 'weblogic.kernel.Default'] WARN

- jsp log4j warn

testlog4j.jsp jsp_servlet.__testlog4j._jspService(__testlog4j.java:141) 2004-09-

20 17:09:23,029 [ExecuteThread: '14' for queue: 'weblogic.kernel.Default'] INFO

- jsp log4j info

 

 

呵呵,enjoy !

 

0 0

相关博文

我的热门文章

img
取 消
img