Web应用中Log4j与JNDI结合1 -- JNDI指定配置文件
2016-05-14 21:00
543 查看
在Tomcat 定制JNDI URL Resource中我介绍了如何在tomcat中实现URL资源访问,在Log4j配置实践中我介绍了在Web应用中如何使用log4j xml配置方式记录日志。这篇我要介绍如何在Web应用中把Log4j和JNDI结合起来,达到可以动态修改Log4j配置文件路径和Log4j日志文件存储路径的目的。
我们知道,Spring本身提供了Log4jConfigListener和Log4jWebConfigurer来帮助初始化Log4j,其中参数log4jConfigLocation可以在classpath或者按指定路径查找log4j配置文件,log4jRefreshInterval指定检查加载配置文件变化。在这里,我们要做的是根据Log4jConfigListener和Log4jWebConfiguer来定制自己的Log4jConfigListener,达到可以按JNDI来访问配置文件的目的。给出实现如下:
Web.xml的配置如下:
我们知道,Spring本身提供了Log4jConfigListener和Log4jWebConfigurer来帮助初始化Log4j,其中参数log4jConfigLocation可以在classpath或者按指定路径查找log4j配置文件,log4jRefreshInterval指定检查加载配置文件变化。在这里,我们要做的是根据Log4jConfigListener和Log4jWebConfiguer来定制自己的Log4jConfigListener,达到可以按JNDI来访问配置文件的目的。给出实现如下:
package com.demo; /*... import ...here ... */ public class JndiLog4jConfigListener implements ServletContextListener { /** Parameter specifying the location of the log4j config file */ public static final String CONFIG_LOCATION_PARAM = "log4jConfigLocation"; /** Parameter specifying the location of the log4j config file */ public static final String CONFIG_FILENAME_PARAM = "log4jConfigFilename"; /** Parameter specifying the refresh interval for checking the log4j config file */ public static final String REFRESH_INTERVAL_PARAM = "log4jRefreshInterval"; /** Parameter specifying whether to expose the web app root system property */ public static final String EXPOSE_WEB_APP_ROOT_PARAM = "log4jExposeWebAppRoot"; public void contextInitialized(ServletContextEvent event) { // Expose the web app root system property. if (exposeWebAppRoot(servletContext)) { WebUtils.setWebAppRootSystemProperty(servletContext); } // Only perform custom log4j initialization in case of a config file. String location = servletContext.getInitParameter(CONFIG_LOCATION_PARAM); String filename = servletContext.getInitParameter(CONFIG_FILENAME_PARAM); if (location != null) { // Perform actual log4j initialization; else rely on log4j's default initialization. try { Context ic = new InitialContext(); location = ((URL)ic.lookup(location)).toExternalForm(); location = location + File.separator + filename; // Write log message to server log. servletContext.log("Initializing log4j from [" + location + "]"); // Check whether refresh interval was specified. String intervalString = servletContext.getInitParameter(REFRESH_INTERVAL_PARAM); if (intervalString != null) { // Initialize with refresh interval, i.e. with log4j's watchdog thread, // checking the file in the background. try { long refreshInterval = Long.parseLong(intervalString); Log4jConfigurer.initLogging(location, refreshInterval); } catch (NumberFormatException ex) { throw new IllegalArgumentException("Invalid 'log4jRefreshInterval' parameter: " + ex.getMessage()); } } else { // Initialize without refresh check, i.e. without log4j's watchdog thread. Log4jConfigurer.initLogging(location); } } catch (Exception ex) { throw new IllegalArgumentException("Invalid log4j Location: " + ex.getMessage()); } } } public void contextDestroyed(ServletContextEvent event) { servletContext.log("Shutting down log4j"); try { Log4jConfigurer.shutdownLogging(); } finally { // Remove the web app root system property. if (exposeWebAppRoot(servletContext)) { WebUtils.removeWebAppRootSystemProperty(servletContext); } } } private static boolean exposeWebAppRoot(ServletContext servletContext) { String exposeWebAppRootParam = servletContext.getInitParameter(EXPOSE_WEB_APP_ROOT_PARAM); return (exposeWebAppRootParam == null || Boolean.valueOf(exposeWebAppRootParam).booleanValue()); } }
Web.xml的配置如下:
<context-param> <param-name>log4jConfigLocation</param-name> <param-value>jndi:java:/comp/env/config_dir</param-value> </context-param> <context-param> <param-name>log4jConfigFilename</param-name> <param-value>log4j-demo.xml</param-value> </context-param> <context-param> <param-name>log4jRefreshInterval</param-name> <param-value>6000</param-value> </context-param> <listener> <listener-class>com.demo.JndiLog4jConfigListener</listener-class> </listener> <resource-ref> <res-ref-name> config_dir </res-ref-name> <res-type> java.net.URL </res-type> <res-auth> Container </res-auth> </resource-ref>
相关文章推荐
- Animation
- Android并发编程之Handler,MessageQueue,Looper详解
- Part2:Unity学习笔记十六 - Space Shooter(从视频最后一课向Done_Main.unity场景修改的过程)
- UVA1587
- 图像处理界面--缩略图的显示
- 最近开发中项目-ZOWEE
- CodeForces 96A Football
- 第7周 C语言程序设计(新2版) 各类位运算符定义
- 在opencv中实现中文输出
- 8086汇编语言课程设计一
- spring版本引起的问题
- 基本的MFC多线程
- 视频摘要视频浓缩
- UIApplication-UI进阶
- 升级Python 版本
- vueJs源码解读0-2
- 关于C语言的问卷调查(补交)
- python冒泡排序实现
- 冒泡排序
- Linux Namespace