posts - 0,  comments - 0,  trackbacks - 0
 

    1.导入包

    2.WEB-INF目录下添加一个配置文件log4j.properties

    log4j.rootLogger=DEBUG,A1,A2

    # 输出到控制台

    log4j.appender.A1=org.apache.log4j.ConsoleAppender

    log4j.appender.A1.layout=org.apache.log4j.PatternLayout

    log4j.appender.A1.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} ["u4fe1"u606f] %m%n

    # 输出到日志文件

    log4j.appender.A2=org.apache.log4j.FileAppender

    log4j.appender.A2.File=${catalina.home}/webapps/test_log4j/logging.log

    log4j.appender.A2.Append=true

    log4j.appender.A2.layout=org.apache.log4j.PatternLayout

    log4j.appender.A2.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} ["u4fe1"u606f] %m%n

    3.JSP中应用Log4j

    <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>

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

    <html>

     <head>

        <title>Test Log4j</title>

     </head>

     <body>

       Test Log4j <br>

       <hr>

       <%

       //载入配置文件

               PropertyConfigurator.configure(request.getRealPath("WEB-INF/log4j.properties"));

       //取得日志记录器

       Logger logger = Logger.getLogger(this.getClass());

               logger.fatal("这是一条从index.jsp产生的fatal信息!");

    logger.error("这是一条从index.jsp产生的error信息!");

    logger.warn("这是一条从index.jsp产生的warn信息!");

    logger.debug("这是一条从index.jsp产生的debug信息!");

    logger.info("这是一条从index.jsp产生的info信息!");

       %>

     </body>

    </html>

    -----------------------

    Log4jServlet中的应用

    package com.yzk.servlet;

    import java.io.IOException;

    import java.io.PrintWriter;

    import javax.servlet.ServletException;

    import javax.servlet.http.HttpServlet;

    import javax.servlet.http.HttpServletRequest;

    import javax.servlet.http.HttpServletResponse;

    import org.apache.log4j.*;

    public class TestLog4j extends HttpServlet {

    Logger logger = null;

    public void doGet(HttpServletRequest request, HttpServletResponse response)

    throws ServletException, IOException {

    response.setContentType("text/html");

    response.setCharacterEncoding("GB18030");

               logger.fatal("这是一条从 TestLog4j 产生的fatal信息!");

    logger.error("这是一条从 TestLog4j 产生的error信息!");

    logger.warn("这是一条从 TestLog4j 产生的warn信息!");

    logger.debug("这是一条从 TestLog4j 产生的debug信息!");

    logger.info("这是一条从 TestLog4j 产生的info信息!");

    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)

    throws ServletException, IOException {

    this.doGet(request, response);

    }

    public void init() throws ServletException {

    PropertyConfigurator.configure(this.getServletContext().getRealPath("WEB-INF/log4j.properties"));

    logger = Logger.getLogger(this.getClass());

    }

    }

    WEB.XML中的配置

     <servlet>

        <servlet-name>TestLog4j</servlet-name>

        <servlet-class>com.yzk.servlet.TestLog4j</servlet-class>

     </servlet>

     <servlet-mapping>

        <servlet-name>TestLog4j</servlet-name>

        <url-pattern>/servlet/TestLog4j</url-pattern>

     </servlet-mapping>

    ---------------------------------------

    修改成在Tomcat启动时自启动日志

    新建一个Servlet来专门做初始化的工作

    import java.io.IOException;

    import java.io.PrintWriter;

    import javax.servlet.ServletException;

    import javax.servlet.http.HttpServlet;

    import javax.servlet.http.HttpServletRequest;

    import javax.servlet.http.HttpServletResponse;

    import org.apache.log4j.*;

    public class InitLog4j extends HttpServlet {

    public void destroy() {

    super.destroy(); // Just puts "destroy" string in log

    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)

    throws ServletException, IOException {

    }

    public void init() throws ServletException {

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

    String confFile = getInitParameter("log4j");

    if (confFile!=null){

    PropertyConfigurator.configure(basePath+confFile);

    }else{

    System.out.println("指定的Log4j配置文件不存在,将影响应用中的日志记录输出!");

    }

    }

    }

    web.xml中的修改配置

     <servlet>

        <servlet-name>TestServlet</servlet-name>

        <servlet-class>TestServlet</servlet-class>

     </servlet>

     <servlet>

        <servlet-name>InitLog4j</servlet-name>

        <servlet-class>InitLog4j</servlet-class>

        <init-param>

                <param-name>log4j</param-name>

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

        </init-param>

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

     </servlet>

     <servlet-mapping>

        <servlet-name>TestServlet</servlet-name>

        <url-pattern>/TestServlet</url-pattern>

     </servlet-mapping>

     <servlet-mapping>

        <servlet-name>InitLog4j</servlet-name>

        <url-pattern>/InitLog4j</url-pattern>

     </servlet-mapping>

    package com.yzk.servlet;

    import java.io.IOException;

    import java.io.PrintWriter;

    import javax.servlet.ServletException;

    import javax.servlet.http.HttpServlet;

    import javax.servlet.http.HttpServletRequest;

    import javax.servlet.http.HttpServletResponse;

    import org.apache.log4j.*;

    public class TestLog4j extends HttpServlet {

    Logger logger = null;

    public void doGet(HttpServletRequest request, HttpServletResponse response)

    throws ServletException, IOException {

    response.setContentType("text/html");

    response.setCharacterEncoding("GB18030");

               logger.fatal("这是一条从 TestLog4j 产生的fatal信息!");

    logger.error("这是一条从 TestLog4j 产生的error信息!");

    logger.warn("这是一条从 TestLog4j 产生的warn信息!");

    logger.debug("这是一条从 TestLog4j 产生的debug信息!");

    logger.info("这是一条从 TestLog4j 产生的info信息!");

    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)

    throws ServletException, IOException {

    this.doGet(request, response);

    }

    public void init() throws ServletException {

    //PropertyConfigurator.configure(this.getServletContext().getRealPath("WEB-INF/log4j.properties"));

    logger = Logger.getLogger(this.getClass());

    }

    }

posted on 2008-12-28 13:09 游上岸的鱼 阅读(191) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航:
 

<2024年11月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

留言簿

文章档案

搜索

  •  

最新评论