Reputation: 1572
How can we specify where log4j has to look at when trying to find its xml configuration file?
It seems that, by default, log4j looks into the root of a class folder, I can say that using the debug log4j functionality and running my application from the IDE.
However, I don't have any class folder in my jar anymore. And the log4j.xml file is at the root of the jar.
I have already tried to set the option -Dlog4j.configuration=log4j.xml but it doesn't work.
Here is the global structure of my application at the moment:
Upvotes: 50
Views: 110080
Reputation: 141
You can use:
DOMConfigurator.configure(log4jConfigurationFilename);
and specify the location if it is other than the root.
Upvotes: 3
Reputation: 81
May be this is very old question, but for me the answer was in the official Log4j 2 documentation:
Automatic Configuration
Log4j has the ability to automatically configure itself during initialization. When Log4j starts it will locate all the
ConfigurationFactory
plugins and arrange them in weighted order from highest to lowest. As delivered, Log4j contains fourConfigurationFactory
implementations: one for JSON, one for YAML, one for properties, and one for XML.
- Log4j will inspect the
"log4j2.configurationFile"
system property and, if set, will attempt to load the configuration using theConfigurationFactory
that matches the file extension. Note that this is not restricted to a location on the local file system and may contain a URL.- If no system property is set the properties ConfigurationFactory will look for
log4j2-test.properties
in the classpath.- If no such file is found the YAML ConfigurationFactory will look for
log4j2-test.yaml
orlog4j2-test.yml
in the classpath.- If no such file is found the JSON ConfigurationFactory will look for
log4j2-test.json
orlog4j2-test.jsn
in the classpath.- If no such file is found the XML ConfigurationFactory will look for
log4j2-test.xml
in the classpath.- If a test file cannot be located the properties ConfigurationFactory will look for
log4j2.properties
on the classpath.- If a properties file cannot be located the YAML ConfigurationFactory will look for
log4j2.yaml
orlog4j2.yml
on the classpath.- If a YAML file cannot be located the JSON ConfigurationFactory will look for
log4j2.json
orlog4j2.jsn
on the classpath.- If a JSON file cannot be located the XML ConfigurationFactory will try to locate
log4j2.xml
on the classpath.- If no configuration file could be located the
DefaultConfiguration
will be used. This will cause logging output to go to the console.
Upvotes: 8
Reputation: 2675
Suppose your log4j configuration is outside the source tree. If it can't find your configuration file, help it with:
-Dlog4j.configuration=file:///your/path/log4j.xml
Note the qualifier file:///. It won't work without.
Upvotes: 25
Reputation: 15
Do not worry about where to put it. Just run your project, say in eclipse, log4j will throw an exception, telling you where it was looking. Then you can put it there.
Once you get it working, you will also figure out how to change that, as you understand the logic.
Upvotes: -1
Reputation: 163
I don't have any error
So log4j is finding your configuration, otherwise you would get:
log4j:WARN No appenders could be found for logger ().
log4j:WARN Please initialize the log4j system properly.
Also:
The log are just printed out on the console
So that means that you probably need to define a file appender, like FileAppender or RollingFileAppender. The "Configuration" section on the short intro has a few examples.
Upvotes: 5
Reputation: 12344
It finds the log4j.xml using the CLASSPATH. If log4j doesn't find any config file, it will send an error to the console. If you don't see any such error then it is likely that it is finding a config file which may not be the one you are editing. There is a command-line option to force Log4J to report the path of the config file it is using.
From http://wiki.apache.org/logging-log4j/Log4jConfigurationHelp
If you run with "-Dlog4j.debug" then log4j will print out info to standard output telling how it is trying to configure itself. Looking through that will tell you where it is looking for the config file.
Upvotes: 39