新聞中心
本篇內(nèi)容介紹了“Log4j中相對(duì)路徑的問題怎么解決”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
創(chuàng)新互聯(lián)服務(wù)項(xiàng)目包括北關(guān)網(wǎng)站建設(shè)、北關(guān)網(wǎng)站制作、北關(guān)網(wǎng)頁(yè)制作以及北關(guān)網(wǎng)絡(luò)營(yíng)銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,北關(guān)網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到北關(guān)省份的部分城市,未來相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
一、properties文件中:
log4j.appender.logfile.File=${WORKDIR}/logs/app.log
其中“${WORKDIR}/”是個(gè)變量,會(huì)被System Property中的“WORKDIR”的值代替。這樣,我們就可以在log4j加載配置文件之前,先用System.setProperty設(shè)置好根路徑。
jsp中可以這樣:
String path = pageContext.getServletContext().getRealPath("/");
String realPath = path+"WEB-INFclasseslog4j.properties";
System.setProperty("WORKDIR",path.replace("","/"));
PropertyConfigurator.configure(realPath.replace("","http://"));//加載.properties文件
[@more@]
二、可以使用環(huán)境變量(沒試過網(wǎng)上看的)
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=${catalina.base}/logs/logs_tomcat.log
log4j.appender.R.MaxFileSize=10KB
三、具體實(shí)現(xiàn):(沒試過網(wǎng)上看的)
一般在我們開發(fā)項(xiàng)目過程中,log4j日志輸出路徑固定到某個(gè)文件夾,這樣如果我換一個(gè)環(huán)境,日志路徑又需要重新修改,比較不方便,目前我采用了動(dòng)態(tài)改變?nèi)罩韭窂椒椒▉韺?shí)現(xiàn)相對(duì)路徑保存日志文件
(1).在項(xiàng)目啟動(dòng)時(shí),裝入初始化類:
public class Log4jInit extends HttpServlet {
static Logger logger = Logger.getLogger(Log4jInit.class);
public Log4jInit() { }
public void init(ServletConfig config) throws ServletException {
String prefix = config.getServletContext().getRealPath("/");
String file = config.getInitParameter("log4j");
String filePath = prefix + file;
Properties props = new Properties();
try {
FileInputStream istream = new FileInputStream(filePath);
props.load(istream);
istream.close();
//toPrint(props.getProperty("log4j.appender.file.File"));
String logFile = prefix + props.getProperty("log4j.appender.file.File");//設(shè)置路徑
props.setProperty("log4j.appender.file.File",logFile); PropertyConfigurator.configure(props);
//裝入log4j配置信息
} catch (IOException e) {
toPrint("Could not read configuration file [" + filePath + "].");
toPrint("Ignoring configuration file [" + filePath + "].");
return;
}
}
public static void toPrint(String content) {
System.out.println(content);
}
}
(2).Web.xml中的配置
“Log4j中相對(duì)路徑的問題怎么解決”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
網(wǎng)頁(yè)標(biāo)題:Log4j中相對(duì)路徑的問題怎么解決
轉(zhuǎn)載注明:http://ef60e0e.cn/article/pogipg.html