Uncategorized

Default log4j initialization procedure

Posted

 

The default initialization algorithm is invoked when the LogManager class is loaded into memory. This class is guaranteed to be loaded before any logger can be used. The exact initialization algorithm is defined as follows:

  • If the log4j.defaultInitOverride system property is set to any other value then “false”, the default initialization procedure (this procedure) is skipped
  • The value of the log4j.configuration system property defines the configuration resource. The value of the log4j.configuration system property can be a URL or a file expressed in a system dependent format.
  • If the log4j.configuration is not defined, then configuration resource log4j.xml is searched with the following algorithm.
    Under JDK 1.2 and later, search for the resource using the thread context class loader. If that fails, attempt to locate the resource using the class loader that loaded the log4j library. Make one last attempt by calling ClassLoader.getSystemResource(resource) method, that is by using the system class loader. The result of the search, if successful, is always a URL
  • If the resource log4j.xml cannot be located, then search for log4j.properties using the same search algorithm.
  • Otherwise, if a configuration resource could be found, invoke the configure(URL) method of the appropriate log4j configurator. If the configuration resource ends with an .xml extension the DOMConfigurator is used. Otherwise, the PropertiesConfigurator is used. The user can optionally specify a custom the same search algorithm.
  • If the log4j.configuration system property was not defined and no resources log4j.xml or log4j.properties could be found, then no default initialization can occur.
  • Otherwise, if a configuration resource could be found, invoke the configure(URL) method of the appropriate log4j configurator. If the configuration resource ends with an .xml extension the DOMConfigurator is used. Otherwise, the PropertiesConfigurator is used.

NOTE The file log4j.xml is probed for in log4j version 1.2.7 and later. Previous log4j versions only probe for the file log4j.properties.