简体   繁体   English

具有Java属性的Log4j手动配置

[英]Log4j Manual Configuration with Java Properties

I'm trying to load log4j.properties( mylog4j.properties ) file manually in Spring web application. 我正在尝试在Spring Web应用程序中手动加载log4j.properties( mylog4j.properties )文件。 The file is located under /WEB-INF/ and I'm reading this file in SYPLogger class as seen below: 该文件位于/ 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);

项目文件资源管理器

Although I tried different location, I couldn't read the file. 尽管我尝试了其他位置,但无法读取文件。 When I gave full path of the configuration file, everything is ok. 当我给出配置文件的完整路径时,一切正常。 But the above code gives FileNotFoundException(The system cannot find the file specified). 但是上面的代码给出了FileNotFoundException(系统找不到指定的文件)。 Please help. 请帮忙。 Thanks in advance. 提前致谢。

Try with the next: 尝试下一个:

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

In another way, have you seen the class org.springframework.web.util.Log4jConfigListener . 换句话说,您是否看到过org.springframework.web.util.Log4jConfigListener类。 You can configure this listener in the web.xml file. 您可以在web.xml文件中配置此侦听器。 eg: 例如:

<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