I am using log4j to log information in my web application. I have chosen log4j.xml type of configuration instead of log4j.properties. I remember that, for log4j.properties configuration, I did not write Java code lines to find the log4j.property file location. However, for log4j.xml file configuration I am specifying explicitly to find it like this
DOMConfigurator.configure("log4j.xml");//it reads file from classpath, working!
Also, tested my application removing the above statement from source. It doesn't see to work. None of the debug statements were printed except
System.out.println();
I have read that log4j by default looks for log4jproperties file or log4j.xml file in the classpath. I checked in the deployed web application, log4j.xml file is in web-inf/classes.
Even though it can't find log4j.xml.
Is it that, the above code line is mandatory for log4j xml configuration in Java?
In fact, can't Java source code doesn't pickup log4j.xml from the classpath without explicit specification as above?
Best Answer
From log4j manual:
So, without code, you can (and should!) define the system property to tell where your config file is. If not, it by default will search for log4j.properties in the classpath. That is the only file it is searching for by default.
System property is usually defined as a startup parameter for your app (app server), something like
-Dlog4j.configuration=path/to/your/config.xml
.