3.String location = servletContext.getInitParameter(CONFIG_LOCATION_PARAM)获取Log4j自定义配置路径,如果不为空解析得到真实路径location = WebUtils.getRealPath(servletContext, location)如下配置
<context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:log4j.properties</param-value> </context-param>配置有两种情况
- `classpath`开头,找到项目类路径,最后用ClassLoader加载,所以不要用"/"开头 <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:log4j.properties</param-value> </context-param>file开头,配置文件具体位置
<context-param> <param-name>log4jConfigLocation</param-name> <param-value>file:///Users/lh/Desktop/log4j.properties</param-value> </context-param>4.读取log4jRefreshInterval属性,表示每隔一段时间,会重新读取配置文件,重新配置Log4j,自动检测更新。会单独启动一个线程来监控定时监控,单位是(ms).配置如下:
<context-param> <param-name>log4jRefreshInterval</param-name> <param-value>2000</param-value> </context-param>5.最后调用log4j自身的API进行配置
if (resolvedLocation.toLowerCase().endsWith(XML_FILE_EXTENSION)) { DOMConfigurator.configure(url); }else { PropertyConfigurator.configure(url); }更多Log4j相关教程见以下内容:
Log4j配置详解
Apache Log4j 2 更多内容请看: