log4j更改配置檔案log4j.properties的預設路徑
log4j的配置檔案放在classpath下會被自動載入,但是更換路徑就不會去載入了。下面給出解決辦法:
1.編寫servlet,並隨著server啟動載入。程式碼如下:
public class FileThreadServlet extends HttpServlet {
publicvoid init(){
String prefix = getServletContext().getRealPath("/");
String log4jFile = getInitParameter("log4jConfigLocation");//web.xml中配置
if (log4jFile !=
PropertyConfigurator.configure(prefix + log4jFile);
}
}
2.在web.xml中配置servlet以及初始化引數,並設定初始啟動
<servlet>
<servlet-name>fileThreadServlet</servlet-name>
<servlet-class>com.test.servlet.FileThreadServlet</servlet-class>
<init-param>
<
<param-value>/WEB-INF/conf/log4j.properties</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
這樣啟動就會直接載入更改路徑後的配置檔案,finish!