JavaWeb应用中初始化Log4j的两种方式
2024-08-25 14:41:33
本文主要介绍了普通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 方法调用之后才被调用。
本篇随笔主要用于个人备忘,如有不对,敬请指出!
最新文章
- ES6入门笔记
- MAC OS升级到10.11(OS X EICAPTION)之后CocoaPods不能正常使用的问题解决
- WebRTC手记之WebRtcVideoEngine2模块
- 【leetcode❤python】 36. Valid Sudoku
- How to enables AX email functionality without Outlook
- Qt 自定义model实现文件系统的文件名排序
- rails3 Bundle简介
- 项目总结——深入浅出socket网络编程
- 杂记之web篇
- mysql子查询慢的问题
- 【ASP.NET 系列】浅谈缓存技术在ASP.NET中的运用
- api大全
- oledb
- H5 18-序选择器
- myeclipse maven的联系
- Cisco VTP中继协议配置
- css列表list、表格table
- maven工程的多环境配置方案(profile)
- 20155209 林虹宇 Exp3 免杀原理与实践
- 在容器服务kubernetes上配置https