我是靠谱客的博主 可耐黑猫,这篇文章主要介绍Web应用中Log4j与JNDI结合1 -- JNDI指定配置文件,现在分享给大家,希望可以做个参考。

在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来访问配置文件的目的。给出实现如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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的配置如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<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>

最后

以上就是可耐黑猫最近收集整理的关于Web应用中Log4j与JNDI结合1 -- JNDI指定配置文件的全部内容,更多相关Web应用中Log4j与JNDI结合1内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(64)

评论列表共有 0 条评论

立即
投稿
返回
顶部