log4j.xml 可以放在2个地方:
1〉使用springframework在web.xml中配置
//设置webAppRootKey的别名,
//如果不设置,在log4j.xml中设置为<param name="File" value="${webapp.root}/WEB-INF/log/frame.log " />
//否则,可以设置为<param name="File" value="${demo.root}/WEB-INF/log/frame.log " />
//如果不使用${webapp.root},就会指向${TOMCAT_HOM},也就是tomcat的安装目录。
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>demo.root</param-value>
</context-param>
//设置配置文件
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.xml</param-value>
</context-param>
//设置监听器
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
这样log4j.xml 就可以WEB-INF里面的任何路径。
Source:
initLogging->
{
WebUtils.setWebAppRootSystemProperty(servletContext);
{
String root = servletContext.getRealPath("/");
String param = servletContext.getInitParameter(WEB_APP_ROOT_KEY_PARAM);
String key = (param != null ? param : DEFAULT_WEB_APP_ROOT_KEY);
System.setProperty(key, root);
//use system property to deal with ${webapp.root} or its alias.
//once I writed careless ${webapp.root} to {webapp.root} ,and look up the error for much time.
}
String location = servletContext.getInitParameter(CONFIG_LOCATION_PARAM);
Log4jConfigurer.initLogging(location);
{
//distinguish the log4j.xml and log4j.properties
if (resolvedLocation.toLowerCase().endsWith(XML_FILE_EXTENSION)) {
DOMConfigurator.configure(url);
}
else {
PropertyConfigurator.configure(url);
}
}
}
use DOMConfigurator.configure("log4j.xml"); to init config ,then LogFactory can work. the "log4j.xml "
may be any name .DOMConfigurator may use cache mechanism as in LogFactory class .cache machanism is
another way to implement "single" design pattern.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2〉WEB-INF/classes下面
采取这种方式,它的文件appender的相对路径总是指向${TOMCAT_HOM},也就是tomcat的安装目录。<param name="File" value="demo.log" /> 会把demo.log 保存到${TOMCAT_HOM}/demo.log ,
采用绝对路径则可以任意设置。
为了使用相对路径,我们把project放到${TOMCAT_HOMe}/webapps下deploy。
如project名为demo,log要保存在demo/log/demo.log文件里,则可以设为:
<param name="File" value="webapps\\demo\\log\\framefile.log" />
---------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------
注意“\" 要用转义字符"\\",如果没加,tomcat启动屏中的路径会变为乱码.
tomcat中似乎采取了某种安全策略,<param name="Append" value="false" />不起作用。
服务器重起后就会新建一个log文件.
posted on 2006-09-14 14:41
R.Zeus 阅读(912)
评论(0) 编辑 收藏 所属分类:
Log4j 、
TOMCAT / RESIN