Where/how does log4j look for a log4j.properties file?
You can enable log4j internal debugging by setting the log4j.debug
system property. Among other things, this will cause log4j to show how it is configuring itself.
You can try explicitly setting the URL to the configuration file with the log4j.configuration
system property.
See also: this question.
Look in the manual under the heading Default Initialization Procedure, where you'll find the following:
The exact default initialization algorithm is defined as follows:
- Setting the
log4j.defaultInitOverride
system property to any other value than "false
" will cause log4j to skip the default initialization procedure (this procedure).- 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 thelog4j.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
".- Attempt to convert the resource variable to a URL.
- 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.- 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 thelog4j.configuratorClass
system property is taken as the fully qualified class name of your custom configurator. The custom configurator you specify must implement theConfigurator
interface.
The problem may be in the classpath
, if the classpath
was defined.
The reason it wasn't loading (in my case): There was a conflicting log4j.properties
file in one of my jars, and it was overloading the one in my classpath
.
In short, if your log4j.properties
file isn't loading, there might be another one somewhere else overriding it.
Just thought I'd throw this in too, in case anyone else runs into this. I just spent the last 5 hours trying to figure out why my default log4j.properties
wouldn't load.
Argh. I discovered the problem was that eclipse had imported the wrong Logger
class. It had imported java.util.logging.Logger which of course has it's own configuration that is different from log4j. Oh well, hope somebody else does this and gets it solved by reading this question.