在web.xml有几个条目和log4j有关,它们是:
[html] view plaincopy
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>petclinic.root</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INFclasseslog4j.properties</param-value>
</context-param>
//(该条目在petclinic中被注释掉了)
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
我们知道,在web-application中使用log4j,有很多配置方式:
a.用servlet或者ServletContextListener对log4j做配置。通常,你不需要亲自编写servlet或者 listener,比如直接利用log4j的com.apache.jakarta.log4j.Log4jInit类,Spring的
org.springframework.web.util.Log4jConfigServlet和 org.springframework.web.util.ServletContextListener,这种方式配置灵活,可以通过参数条目自行指定log4j.properties的位置。
b.把log4j 的默认配置文件(log4j.properties)放在classpath中,通常是/web-inf/classes目录下.这种方式是log4j的 默认配置方式,无须其他配置。缺点就是无法灵活的通过配置文件来指定log4j.properties的文件位置。在我们的petclinic项目中,因 为listener条目被注释,所以采用的也是这种缺省方式。
现在我们考虑listener条目没有被注释的情况,这种情况和注册Log4jConfigServlet的目的是一样的,只是必须在支持listener的servlet container中使用。找到Log4jConfigServlet和ServletContextListener的源码,他们都在适当的地方(callback method)调用了Log4jWebConfigurer.initLogging(getServletContext()); 定位到这个方法,第一句就是:WebUtils.setWebAppRootSystemProperty(servletContext);再定位到该 方法,方法很短:
[java] view plaincopy
public static void setWebAppRootSystemProperty(ServletContext servletContext) throws IllegalStateException {
String param = servletContext.getInitParameter(WEB_APP_ROOT_KEY_PARAM);
String key = (param != null ? param : DEFAULT_WEB_APP_ROOT_KEY);
String oldValue = System.getProperty(key);
if (oldValue != null) {
throw new IllegalStateException("WARNING: Web app root system property already set: " + key + " = " + oldValue + " - Choose unique webAppRootKey values
in your web.xml files!");
}
String root = servletContext.getRealPath("/");
if (root == null) {
throw new IllegalStateException("Cannot set web app root system property when WAR file is not expanded");
}
System.setProperty(key, root);
servletContext.log("Set web app root system property: " + key + " = " + root);
}
从代码看出,该方法其实就是把该web application的根目录的绝对文件路径作为属性保存在 System的属性列表中。该属性的名字,由web.xml文件中的名为"webAppRootKey"的参数值指出。如果不在web.xml中定义 webAppRootKey参数,那么属性名就是缺省的"webapp.root".在我们的petclinic项目中已经定义了 webAppRootKey参数,其值为"petclinic.root",因此,属性的名字就是"petclinic.root".
再回到Log4jWebConfigurer.initLogging(getServletContext()),接下来的行为是从web.xml中获取 log4jConfigLocation和log4jRefreshInterval.前者指出log4j配置文件(有可能是xml格式)的位置和名字, 后者则指出重新都取配置文件的时间间隔.然后调用Log4jConfigurer.initLogging()方法对log4j做配置。从 Log4jConfigurer.initLogging()方法我们可以看到,针对不同格式的配置文件(properties或者xml), Log4jConfigurer采用不同的lo4j configurator类做配置,例如DOMConfigurator或者 PropertyConfigurator。
至此,关于petclinic中关于log4j的配置,我们已经基本上弄清楚了。可是System对象中的petclinic.root属性在什么时候使用呢?在web-inf/classes下面的log4j.properties文件中,有这么一句:log4j.appender.logfile.File=${petclinic.root}/WEB-INF/petclinic.log这 样,我们就用上了petclinic.root属性了。从上面的分析可知,如果你不在web.xml中定义webAppRootKey参数,那么你就得把 log4j.appender.logfile.File=${petclinic.root}/WEB-INF/petclinic.log中的petclinic.root变量改为webapp.root变量或者干脆换成别的绝对路径了。
分享到:
相关推荐
log4j配置文档log4j配置文档log4j配置文档log4j配置文档log4j配置文档log4j配置文档log4j配置文档log4j配置文档log4j配置文档log4j配置文档log4j配置文档log4j配置文档
log4j配置和加载方法log4j配置和加载方法
log4j 详细配置 log4j 详细配置 log4j 详细配置 log4j 详细配置
Log4j2学习笔记,引入log4j2的依赖-log4j2.xml配置模板-application.properties文件配置-使用
Log4j配置说明.rarLog4j配置说明.rarLog4j配置说明.rarLog4j配置说明.rar,Log4j配置,Log4j Log4j配置说明.rarLog4j配置说明.rarLog4j配置说明.rarLog4j配置说明.rar,Log4j配置,Log4j
log4j中配置日志文件相对路径方法分析 方法一、 解决的办法自然是用相对路径代替绝对路径,其实log4j的FileAppender本身就有这样的机制,如:log4j.appender.logfile.File=${WORKDIR}/logs/app.log 其中“${...
常用log4j配置 [转] 常用log4j配置,
tomcat下的log4j日志配置,给tomcat配置log4j有好几种方法,我知道的有: 一、tomcat级别的统一日志管理 二、每个webapp分别配置log4j
log4j配置文件以及配置文件说明 包里有一个log4j配置文件 和一个配置详解
log4j2配置,文件分割,自动清除等相关配置。
主要讲述log4j的简介,以及log4j的配置和在实际应用的使用。如果您不知道如何配置log4j,那么这本书将对您有很大的帮助。
SpringBoot框架配置log4j和log4j2的配置代码,项目中用到的配置文件,可下载使用
log4j 2.x版本不再支持像1.x中的.properties后缀的文件配置方式,2.x版本配置文件后缀名只能为".xml",".json"或者".jsn". 系统选择配置文件的优先级(从先到后)如下: (1).classpath下的名为log4j2-test.json...
完整的log4j配置文件
Log4j配置详解 特别清晰,大家可以下载来参考学习,有问题可以反馈
概述了log4j的基本配置信息。很适合初学者学习log4j的配置使用。
Log4j日志配置说明,Log4j日志配置说明Log4j日志配置说明,Log4j日志配置说明
log4j配置文件例子,可以让你轻松配置log4j
log4j2 xml 和 properties 配置
本文档提供log4j配置相对路径三种方法:log4j.properties文件配置、xml文件配置和通过servlet初始化方式来配置,希望对大家有所帮助