Reputation: 156
I'm trying to load log4j.properties(mylog4j.properties) file manually in Spring web application. The file is located under /WEB-INF/ and I'm reading this file in SYPLogger class as seen below:
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). Please help. Thanks in advance.
Upvotes: 0
Views: 590
Reputation: 42020
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
. You can configure this listener in the web.xml
file. e.g.:
<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>
Upvotes: 1