我正在使用log4j來記錄我的應用程序中的活動。我想從數據庫中取得日誌路徑。 現在我需要動態配置我的log4j屬性。正在更改日誌路徑
,我們可以做到這一點上,我們改變log4h日誌路徑飛..
請建議。
感謝
我正在使用log4j來記錄我的應用程序中的活動。我想從數據庫中取得日誌路徑。 現在我需要動態配置我的log4j屬性。正在更改日誌路徑
,我們可以做到這一點上,我們改變log4h日誌路徑飛..
請建議。
感謝
創建單獨的屬性文件來保存具體環境相關的設置,例如:
**uatLog4j.properties**
#######################
UAT Settings
#######################
{Add your Settings here}
,另一個爲SY生產環境。
**productionLog4j.properties**
########################
PRODUCTION settings
########################
{Add your Settings here}
,然後用說IP地址或服務器名稱確定部署的平臺,從數據庫傳遞到所需的環境屬性文件的路徑爲所需。
或者,您可以使用LogManager
來檢索Logger實例或在當前的LoggerRepository上進行操作。請參閱Javadoc和RepositorySelecter示例。
注意: 您可以使用XML實現相同。
您應該創建一個類,在啓動時加載並配置log4j。 以下是我在JavaEE的項目中使用的代碼,什麼加載從外部目錄中的配置文件:
public class InitListener implements ServletContextListener {
public InitListener() {
}
public void contextInitialized(ServletContextEvent sce) {
try {
File file = null;
file = new File(System.getProperty("catalina.base") + "/conf/query-log4j.xml");
DOMConfigurator.configure(file.toURL());
System.out.println("Log4J successfully configured!");
} catch(Exception e) {
System.out.println("There was an error when initialize the Log4J config!");
e.printStackTrace();
throw new RuntimeException(e);
}
}
public void contextDestroyed(ServletContextEvent sce) {
}
}
嗨,Thannks的答覆,我的實際問題是如何更改日誌路徑上飛。我無法在環境中硬編碼路徑。 – Pedantic
如果您使用MentaLog,所有你需要做的是:
yourLogger.setFilename("newfilenamehere.log");
您的日誌會自動以新名稱重新打開。我個人認爲,programmatic configuration是通過XML和/或註釋的方式。它提供了無與倫比的靈活性和易用性。
請參閱http://stackoverflow.com/questions/4598702/dynamically-changing-log4j-log-level以獲取動態更改日誌配置的一些方法。 – DNA