log4j_properties配置生成log檔案路徑問題
阿新 • • 發佈:2019-01-03
那麼hibernate.log這個檔案會很“聰明地”建立到Tomcat的啟動目錄上,如果我希望它在\WEB-INF\下面安家,就要這樣寫:
log4j.appender.file.File=c:\tomcat41\webapps\mysite\WEB-INF\hibernate.log
但是如果我要把它打包釋出到虛擬主機上,那裡的linux系統要求我把這行寫成:
log4j.appender.file.File=/user/mysite.com/WEB-INF/hibernate.log
有沒有個好辦法讓這個日誌檔案不要到處亂跑,就固定在WEB-INF這個相對路徑下?
方法一:
可以在web.xml中寫
<servlet>
<servlet-name>log4j-init</servlet-name>
<servlet-class>com.kevin.myapp.common.servlet.Log4JInit</servlet-class>
<init-param>
<param-name>log4j-init-file</param-name>
<param-value>/WEB-INF/config/log4j.properties</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
---------------------------------------
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.PropertyConfigurator;
public class Log4JInit extends HttpServlet{
public void init(){
String prifix = getServletContext().getRealPath("/");
String file = getInitParameter("log4j-init-file");
if(file != null){
Properties prop = new Properties();
try{
prop.load(new FileInputStream(prifix+file));
prop.setProperty("log4j.appender.R.File",
prifix + prop.getProperty("log4j.appender.R.File"));
PropertyConfigurator.configure(prop);
}catch(IOException e){
e.printStackTrace();
}
}
}
public void doGet(HttpServletRequest request,HttpServletResponse response){
}
}
方法二:
它放到tomcat的logs目錄下就可以了
log4j.appender.R.File=../logs/log4j.log
方法三:
可以在system的變數裡面增加 webroot等變數
然後再log4j.properties裡面
log4j.appender.R.File=${webroot}/logs/log4j.log
log4j.appender.file.File=c:\tomcat41\webapps\mysite\WEB-INF\hibernate.log
但是如果我要把它打包釋出到虛擬主機上,那裡的linux系統要求我把這行寫成:
log4j.appender.file.File=/user/mysite.com/WEB-INF/hibernate.log
有沒有個好辦法讓這個日誌檔案不要到處亂跑,就固定在WEB-INF這個相對路徑下?
方法一:
可以在web.xml中寫
<servlet>
<servlet-name>log4j-init</servlet-name>
<servlet-class>com.kevin.myapp.common.servlet.Log4JInit</servlet-class>
<init-param>
<param-name>log4j-init-file</param-name>
<param-value>/WEB-INF/config/log4j.properties</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
---------------------------------------
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.PropertyConfigurator;
public class Log4JInit extends HttpServlet{
public void init(){
String prifix = getServletContext().getRealPath("/");
String file = getInitParameter("log4j-init-file");
if(file != null){
Properties prop = new Properties();
try{
prop.load(new FileInputStream(prifix+file));
prop.setProperty("log4j.appender.R.File",
prifix + prop.getProperty("log4j.appender.R.File"));
PropertyConfigurator.configure(prop);
}catch(IOException e){
e.printStackTrace();
}
}
}
public void doGet(HttpServletRequest request,HttpServletResponse response){
}
}
方法二:
它放到tomcat的logs目錄下就可以了
log4j.appender.R.File=../logs/log4j.log
方法三:
可以在system的變數裡面增加 webroot等變數
然後再log4j.properties裡面
log4j.appender.R.File=${webroot}/logs/log4j.log