How can we specify were 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,
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.
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.
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.
May be this is very old question, but for me the answer was here https://logging.apache.org/log4j/2.x/manual/configuration.html
As per above link, Apache log search in classpath for the log4j2.xml files in a specific order as below
You can use:
DOMConfigurator.configure(log4jConfigurationFilename);
and specify the location if it is other than the root!
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.