这个类是 org.springframework.web.util.Log4jConfigListener
这个类通过监听器将应用程序的路径设到System的property里,从而可以将代表应用程序路径的property作为log4j的输出路径
log4j.appender.R.File=${webapp.root}/log/log.log
source code :
public static final String WEB_APP_ROOT_KEY_PARAM = "webAppRootKey" ; //web.xml的context-param
public static final String DEFAULT_WEB_APP_ROOT_KEY = "webapp.root" ; //web.xml默认的context-key
String root = servletContext.getRealPath( "/" ); //获取应用程序路径
......
String param = servletContext.getInitParameter(WEB_APP_ROOT_KEY_PARAM); //根据context-param获取context-key
String key = (param != null ? param : DEFAULT_WEB_APP_ROOT_KEY); //context-key即是那个变量代表应用程序路径
......
System.setProperty(key, root); //将context-key和应用程序路径保存至System的property里
要使用当前web路径,只需要在web.xml配置一个监听器即可
< listener >
< listener-class >org.springframework.web.util.Log4jConfigListener</ listener-class >
</ listener >
(注意:此监听器要在Spring容器context配置之前,否则不起作用,因为加载ContextLoaderListener时,系统还没有加载Log4jConfigListener,所以不会去找log4j.property,所以监听器一定要在Spring容器启动前)
通过此监听器,log4j的配置文件即可使用webapp.root日志文件的输出目录
默认是web路径是webapp.root,也可以通过以下web.xml配置指定
< context-param >
< param-name >webAppRootKey</ param-name >
< param-value >xxx.xxx</ param-value >
</ context-param >
以上就是极悦小编介绍的"Log4j日志文件输出路径设置",希望对大家有帮助,想了解更多可查看log4j教程。极悦在线学习教程,针对没有任何Java基础的读者学习,让你从入门到精通,主要介绍了一些Java基础的核心知识,让同学们更好更方便的学习和了解Java编程,感兴趣的同学可以关注一下。
你适合学Java吗?4大专业测评方法
代码逻辑 吸收能力 技术学习能力 综合素质
先测评确定适合在学习