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

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

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

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

  web.xml 编写如下:  

 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>initServlet</servlet-name>
<servlet-class>com.michael.controll.InitServlet</servlet-class>
<init-param>
<param-name>log4j</param-name>
<param-value>WEB-INF/config/log4j.properties</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>initServlet</servlet-name>
<url-pattern>/initServlet.do</url-pattern>
</servlet-mapping> </web-app>

  

其中参数 <load-on-startup>1</load-on-startup> 指定 initServlet 随 web 应用的部署而自启动,启动优先级为 1,此数值越小,优先级越高。
并在参数 <param-value>WEB-INF/config/log4j.properties</param-value> 中指定 log4j.properties 文件存放的位置。
InitServlet 代码如下:
 public class InitServlet extends HttpServlet {
@Override
public void init() throws ServletException {
super.init();
String prefix = getServletContext().getRealPath("/");
// Log4J
String log4jFile = getServletConfig().getInitParameter("log4j");
String log4jConfigPath = prefix + log4jFile;
PropertyConfigurator.configure(log4jConfigPath);
}
}

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

再来看下方式二,通过监听器 ServletContextListener 监听 ServletContext 的初始化事件来初始化 Log4j 。
web.xml 代码如下:
 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1"> <context-param>
<param-name>log4j</param-name>
<param-value>WEB-INF/config/log4j.properties</param-value>
</context-param> <listener>
<listener-class>com.michael.listener.MyServletContextListener</listener-class>
</listener> </web-app>
同样在 servletContext 初始化参数 <param-value>WEB-INF/config/log4j.properties</param-value> 中指定文件存放位置。
下面看下监听 servletContext 初始化的监听器:
 public class MyServletContextListener implements ServletContextListener {

     @Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
ServletContext ctx = servletContextEvent.getServletContext();
String prefix = ctx.getRealPath("/");
// Log4J
String log4jFile = ctx.getInitParameter("log4j");
String log4jConfigPath = prefix + log4jFile;
PropertyConfigurator.configure(log4jConfigPath);
System.out.println("initialized log4j finish");
} @Override
public void contextDestroyed(ServletContextEvent servletContextEvent) { }
}

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

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

ServletContextListener 的 contextInitialized 方法调用之后才被调用。
  
  本篇随笔主要用于个人备忘,如有不对,敬请指出!
 

最新文章

  1. ES6入门笔记
  2. MAC OS升级到10.11(OS X EICAPTION)之后CocoaPods不能正常使用的问题解决
  3. WebRTC手记之WebRtcVideoEngine2模块
  4. 【leetcode❤python】 36. Valid Sudoku
  5. How to enables AX email functionality without Outlook
  6. Qt 自定义model实现文件系统的文件名排序
  7. rails3 Bundle简介
  8. 项目总结——深入浅出socket网络编程
  9. 杂记之web篇
  10. mysql子查询慢的问题
  11. 【ASP.NET 系列】浅谈缓存技术在ASP.NET中的运用
  12. api大全
  13. oledb
  14. H5 18-序选择器
  15. myeclipse maven的联系
  16. Cisco VTP中继协议配置
  17. css列表list、表格table
  18. maven工程的多环境配置方案(profile)
  19. 20155209 林虹宇 Exp3 免杀原理与实践
  20. 在容器服务kubernetes上配置https

热门文章

  1. How To Install Git on CentOS 7
  2. HIVE UDF
  3. Python中正则表达式对中文的匹配问题
  4. linux进行Java开发环境的部署
  5. 游戏引擎架构Note2
  6. 专利系统数据库连接出现 base-64字符串中的无效字符 错误
  7. Hadoop之MapReduce(二)序列化,排序及分区
  8. codeforces:Helga Hufflepuff&#39;s Cup
  9. C++——STL容器
  10. 关于equal和toString方法的实验报告