Log4j中使用相对路径的方法
来源:百度文库 编辑:神马文学网 时间:2024/05/02 00:42:33
下面以xml格式的log4j配置文件为例说明
view plaincopy to clipboardprint?
1 在log4j.xml配置文件中以 ${变量名} 写路径
2 在初始化log4j之前设置系统变量
3 上面d:/webroot/log4j 是写死了的, 但这个路径可以根据实际情况动态获得 比如你想得到WEB-INF的上级路径,可以这么写 这样解决相对web路径的问题
view plaincopy to clipboardprint?
public String getWebRootPath(ServletContextEvent sce) {
return sce.getServletContext().getRealPath("/");
}
public String getWebRootPath(ServletContextEvent sce) {
return sce.getServletContext().getRealPath("/");
}4 很多人喜欢直接把log4j.xml或log4j.properties文件放到classes下而不做DOMConfigurator.configure(log4jfile);这个操作,这时候可以在启动时可使用 java -Dlog4jdir=路径
view plaincopy to clipboardprint?
System.setProperty("log4jdir","d:/webroot/log4j");
String log4jfile = "log4j.xml";
DOMConfigurator.configure(log4jfile);