簡體   English   中英

具有Java屬性的Log4j手動配置

[英]Log4j Manual Configuration with Java Properties

我正在嘗試在Spring Web應用程序中手動加載log4j.properties( mylog4j.properties )文件。 該文件位於/ WEB-INF /下 ,我正在SYPLogger類中讀取此文件,如下所示:

private static final String CONF_FILE_NAME = "mylog4j.properties";             
Properties props = new Properties();
props.load(new FileInputStream(CONF_FILE_NAME));
PropertyConfigurator.configure(props);

項目文件資源管理器

盡管我嘗試了其他位置,但無法讀取文件。 當我給出配置文件的完整路徑時,一切正常。 但是上面的代碼給出了FileNotFoundException(系統找不到指定的文件)。 請幫忙。 提前致謝。

嘗試下一個:

String path = Thread.currentThread()
                    .getContextClassLoader()
                    .getResource("/")
                    .toURI()
                    .resolve("../mylog4j.properties")
                    .getPath();
Properties props = new Properties();
props.load(new FileInputStream(path));
PropertyConfigurator.configure(props);

換句話說,您是否看到過org.springframework.web.util.Log4jConfigListener類。 您可以在web.xml文件中配置此偵聽器。 例如:

<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>/WEB-INF/mylog4j.properties</param-value>
</context-param>
<listener>
    <listener-class>
        org.springframework.web.util.Log4jConfigListener
    </listener-class>
</listener>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM