简体   繁体   中英

Ways to configure Log4j XML Configuration

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?

From log4j manual :

The exact default initialization algorithm is defined as follows:

  1. Setting the log4j.defaultInitOverride system property to any other value then "false" will cause log4j to skip the default initialization procedure (this procedure).

  2. Set the resource string variable to the value of the log4j.configuration system property. The preferred way to specify the default initialization file is through the log4j.configuration system property. In case the system property log4j.configuration is not defined, then set the string variable resource to its default value "log4j.properties".

  3. Attempt to convert the resource variable to a URL.

  4. If the resource variable cannot be converted to a URL, for example due to a MalformedURLException, then search for the resource from the classpath by calling org.apache.log4j.helpers.Loader.getResource(resource, Logger.class) which returns a URL. Note that the string "log4j.properties" constitutes a malformed URL. See Loader.getResource(java.lang.String) for the list of searched locations.

  5. If no URL could not be found, abort default initialization. Otherwise, configure log4j from the URL. The PropertyConfigurator will be used to parse the URL to configure log4j unless the URL ends with the ".xml" extension, in which case the DOMConfigurator will be used. You can optionaly specify a custom configurator. The value of the log4j.configuratorClass system property is taken as the fully qualified class name of your custom configurator. The custom configurator you specify must implement the Configurator interface.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM