本文主要介紹了普通JavaWeb應用(基於Tomcat)中初始化Log4j的兩種方式:java
1、經過增長 InitServlet ,設置令其自啓動來初始化 Log4j 。web
2、經過監聽器 ServletContextListener 監聽 ServletContext 的初始化事件來初始化 Log4j 。app
先來看下方式一,直接上代碼:ide
web.xml 編寫以下: url
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 的初始化工做。spa
再來看下方式二,經過監聽器 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 的初始化。code
值得注意的是,不管在 <load-on-startup>1</load-on-startup> 參數中把自啓動 servlet 的優先級設置的多高,這個 servlet 的 init 方法都會在xml
ServletContextListener 的 contextInitialized 方法調用以後才被調用。 本篇隨筆主要用於我的備忘,若有不對,敬請指出!