中選擇其配置有沒有辦法找出Log4J從哪裏挑選配置文件? 我試圖更改我的log4j.xml,並且這些更改未反映在Log4j行爲中。我刪除了log4j.xml,有趣的是,Log4J仍然在處理舊的行爲。所以它必須選擇一些在我的命名空間中可用的配置文件。但問題是我怎樣才能找出哪一個。有沒有辦法做到這一點?有很多不同的jar文件依賴關係,所以它們中的一個必須包含一個log4j.xml或log4j.properties來覆蓋我的更改。 任何想法?如何知道Log4j在何處從
3
A
回答
7
運行應用程序時,您可以設置系統屬性-Dlog4j.debug
。 log4j的會產生調試輸出在這種情況下,告訴你它是如何解析到的log4j.xml的路徑,例如:
log4j: Trying to find [log4j.xml] using context classloader [email protected]e7.
log4j: Using URL [file:/C:/develop/workspace/foobar/target/classes/log4j.xml] for automatic log4j configuration.
log4j: Preferred configurator class: org.apache.log4j.xml.DOMConfigurator
要設置的路徑明確使用的系統屬性-Dlog4j.configuration=file:c:/pathToFile
描述here。
0
激活-Dlog4j.debug
就像FrVaBe建議的his answer是一個很好的解決方案。但是,我想在啓動時打印或記錄位置,而不激活log4j的調試模式。我創建了一個小的util方法,它主要是log4j的默認init例程的副本,它返回配置文件的URL。
也許這是對別人有用:
/**
* Get the URL of the log4j config file. This is mostly copied from org.apache.log4j.LogManager's default init routine.
*
* @return log4j config url
*/
public static URL getLog4JConfigurationUrl(){
/** Search for the properties file log4j.properties in the CLASSPATH. */
String override = OptionConverter.getSystemProperty(LogManager.DEFAULT_INIT_OVERRIDE_KEY, null);
// if there is no default init override, then get the resource
// specified by the user or the default config file.
if (override == null || "false".equalsIgnoreCase(override)){
String configurationOptionStr = OptionConverter.getSystemProperty(LogManager.DEFAULT_CONFIGURATION_KEY, null);
URL url;
// if the user has not specified the log4j.configuration
// property, we search first for the file "log4j.xml" and then
// "log4j.properties"
if (configurationOptionStr == null){
url = Loader.getResource("log4j.xml");
if (url == null){
url = Loader.getResource(LogManager.DEFAULT_CONFIGURATION_FILE);
}
} else {
try {
url = new URL(configurationOptionStr);
} catch (MalformedURLException ex) {
// so, resource is not a URL:
// attempt to get the resource from the class path
url = Loader.getResource(configurationOptionStr);
}
}
if (url == null)
throw new RuntimeException("log4j configuration could not be found!");
return url;
}
throw new RuntimeException("default init is overridden, log4j configuration could not be found!");
}
PS:如果你知道一種方式來問log4j來進行它目前使用的配置文件,讓我知道。
相關問題
- 1. 如何知道log4j輸出的來源?
- 2. 如何知道在處理2
- 3. 我們如何知道log4j屬性文件在哪裏?
- 4. 如何知道在log4j中,ERROR級別是否被觸發?
- 5. MVC:如何知道從HttpPostedFileBase
- 6. Cloud.orbit.exception.UncheckedException:不知道如何處理狀態
- 7. C#不知道如何處理System.Web.HttpResponse
- 8. 我不知道如何處理這個
- 9. PHP/SQL不知道如何處理
- 10. Erlang「badarg」,不知道如何處理
- 11. 如何知道
- 12. 如何知道
- 13. 如何知道
- 14. JVM如何知道在運行時何處捕獲異常?
- 15. 如何知道在Android
- 16. 如何知道在SAS
- 17. 如何知道MRO在python2
- 18. 如何知道在LINQ
- 19. 如何知道在Magento CE
- 20. 如何知道在Rx.Observable.combineLatest
- 21. 如何知道在linux
- 22. 如何知道,是Windows後臺處理程序empy,從.bat?
- 23. Docker知道在何處從構建Dockerfile時拉動.sh腳本?
- 24. 如何知道onActivityResult
- 25. 如何知道android
- 26. 如何知道TTF
- 27. 如何知道iPhoneSDK
- 28. 如何知道笨
- 29. 如何知道.NET
- 30. 如何知道'str'何時能夠處理unicode,何時不能?