Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force log4j load xml config from non src folder?

Tags:

java

xml

log4j

Is it possible to have log4j.xml loaded from a different dir than source root and how? (programmatically?) Meaning that it's somewhere in FS not just in classpath.

like image 487
Denys S. Avatar asked Jul 14 '11 07:07

Denys S.


People also ask

Can we use log4j without properties file?

println() over Log4j, of course for testing purposes, because it doesn't require any configuration, you can just use it, without bothering about XML or properties file configuration, but the most programmer will agree that they would prefer to use Log4j over println statements, even for test programs if it's easy to ...


2 Answers

Using DOMConfigurator you can specify the the XML file used to configure log4j.

DOMConfigurator.configure("/path/to/log4j.xml");

For log4j.properties you can do the same thing with PropertyConfigurator.

PropertyConfigurator.configure("/path/to/log4j.properties");
like image 87
Mark Avatar answered Oct 10 '22 20:10

Mark


This might not be the best way, but you can specify the location of the file by setting the java property log4j.configuration=path/to/config/file. For example, you can specify this directly to the java command with java -Dlog4j.configuration=path.

like image 36
Jon7 Avatar answered Oct 10 '22 20:10

Jon7