zoukankan      html  css  js  c++  java
  • JavaWeb应用中初始化Log4j的两种方式

    本文主要介绍了普通JavaWeb应用(基于Tomcat)中初始化Log4j的两种方式:

      1、通过增加 InitServlet ,设置令其自启动来初始化 Log4j 。

      2、通过监听器 ServletContextListener 监听 ServletContext 的初始化事件来初始化 Log4j 。

    先来看下方式一,直接上代码:

      web.xml 编写如下:  

     1 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     2          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     3          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
     4          version="3.1">
     5     <servlet>
     6         <servlet-name>initServlet</servlet-name>
     7         <servlet-class>com.michael.controll.InitServlet</servlet-class>
     8         <init-param>
     9             <param-name>log4j</param-name>
    10             <param-value>WEB-INF/config/log4j.properties</param-value>
    11         </init-param>
    12         <load-on-startup>1</load-on-startup>
    13     </servlet>
    14 
    15     <servlet-mapping>
    16         <servlet-name>initServlet</servlet-name>
    17         <url-pattern>/initServlet.do</url-pattern>
    18     </servlet-mapping>
    19 
    20 </web-app>

      

    其中参数 <load-on-startup>1</load-on-startup> 指定 initServlet 随 web 应用的部署而自启动,启动优先级为 1,此数值越小,优先级越高。
    并在参数 <param-value>WEB-INF/config/log4j.properties</param-value> 中指定 log4j.properties 文件存放的位置。
    InitServlet 代码如下:

     1 public class InitServlet extends HttpServlet {
     2     @Override
     3     public void init() throws ServletException {
     4         super.init();
     5         String prefix = getServletContext().getRealPath("/");
     6         // Log4J
     7         String log4jFile = getServletConfig().getInitParameter("log4j");
     8         String log4jConfigPath = prefix + log4jFile;
     9         PropertyConfigurator.configure(log4jConfigPath);
    10     }
    11 }

      这样即可完成 log4j 的初始化工作。

    再来看下方式二,通过监听器 ServletContextListener 监听 ServletContext 的初始化事件来初始化 Log4j 。
    web.xml 代码如下:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
     5          version="3.1">
     6 
     7     <context-param>
     8         <param-name>log4j</param-name>
     9         <param-value>WEB-INF/config/log4j.properties</param-value>
    10     </context-param>
    11     
    12     <listener>
    13         <listener-class>com.michael.listener.MyServletContextListener</listener-class>
    14     </listener>
    15 
    16 </web-app>
    同样在 servletContext 初始化参数 <param-value>WEB-INF/config/log4j.properties</param-value> 中指定文件存放位置。
    下面看下监听 servletContext 初始化的监听器:

     1 public class MyServletContextListener implements ServletContextListener {
     2 
     3     @Override
     4     public void contextInitialized(ServletContextEvent servletContextEvent) {
     5         ServletContext ctx = servletContextEvent.getServletContext();
     6         String prefix = ctx.getRealPath("/");
     7         // Log4J
     8         String log4jFile = ctx.getInitParameter("log4j");
     9         String log4jConfigPath = prefix + log4jFile;
    10         PropertyConfigurator.configure(log4jConfigPath);
    11         System.out.println("initialized log4j finish");
    12     }
    13 
    14     @Override
    15     public void contextDestroyed(ServletContextEvent servletContextEvent) {
    16 
    17     }
    18 }

      在 context 初始化完成后调用 contextInitialized 方法完成 Log4j 的初始化。

      值得注意的是,无论在 <load-on-startup>1</load-on-startup> 参数中把自启动 servlet 的优先级设置的多高,这个 servlet  的 init 方法都会在

    ServletContextListener 的 contextInitialized 方法调用之后才被调用。
      
      本篇随笔主要用于个人备忘,如有不对,敬请指出!
     
  • 相关阅读:
    uni-app中showModel会阻碍 uni.navigateBack跳转
    vue中使用Bus
    vue中class动态绑定值拼接字符串
    使用moment格式化
    sublimit中智能提示插件的安装
    element-table
    全球十大顶级俱乐部
    java的几种对象(PO,VO,DAO,BO,POJO)解释
    软件工程术语(上)
    职场秘笈:聪明人离职后必做的5件事
  • 原文地址:https://www.cnblogs.com/Michaelwjw/p/6655490.html
Copyright © 2011-2022 走看看