Where to place log4j.xml

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, 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 :

  • com
  • lib
  • meta-inf
  • log4j.xml

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.

log4j.xml example, Create this file and put in application classpath. log4j.xml. <! DOCTYPE log4j:​configuration SYSTEM "log4j.dtd">. It is true that the files get placed inside the jar file, but my Spring applicationContext.xml file has the log4j.xml file in 'src/log4j.xml'. Clearly, when I deploy on another machine, the 'src' file doesn't get copied over.


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.

Setting Up Logging, This logging system is configured by placing the log4j.xml file in the web files to be created in a non-default location, specify the path for the log file location. This example demonstrated how to configure Log4j setup using the Properties file and XML file.These are the two most widely used techniques for configuring the Log4j for your application.


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.

Log4j – Frequently Asked Questions, By default, Log4j looks for a configuration file named log4j2.xml (not log4j.xml) in the classpath. Maven by default places all files from src/main/resources into /WEB-INF/classes/.With log4j, you can specify where your configuration file is located with help of Web App Deployment Descriptor (web.xml).


You can use:

DOMConfigurator.configure(log4jConfigurationFilename);

and specify the location if it is other than the root!

Log4j – Configuring Log4j 2, Identifies the location for the classloader to located the XML Schema to use to validate the configuration. Only valid when strict is set to true. If not set no schema​  there is a possibility of a load a parameter file within properties the log4j.xml . I am using WebLogic Server within the server and have the search.properties file within the file and have routAux = / my / dir / out / so that in the log4j.xml can do something like this. within the log4j.xml x.


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

  1. Log4j will inspect the "log4j.configurationFile" system property and, if set, will attempt to load the configuration using the ConfigurationFactory that matches the file extension.
  2. If no system property is set the properties ConfigurationFactory will look for log4j2-test.properties in the classpath.
  3. If no such file is found the YAML ConfigurationFactory will look for log4j2-test.yaml or log4j2-test.yml in the classpath.
  4. If no such file is found the JSON ConfigurationFactory will look for log4j2-test.json or log4j2-test.jsn in the classpath. If no such file is found the XML ConfigurationFactory will look for log4j2-test.xml in the classpath.
  5. If a test file cannot be located the properties ConfigurationFactory will look for log4j2.properties on the classpath.
  6. If a properties file cannot be located the YAML ConfigurationFactory will look for log4j2.yaml or log4j2.yml on the classpath.
  7. If a YAML file cannot be located the JSON ConfigurationFactory will look for log4j2.json or log4j2.jsn on the classpath.
  8. If a JSON file cannot be located the XML ConfigurationFactory will try to locate log4j2.xml on the classpath.
  9. If no configuration file could be located the DefaultConfiguration will be used. This will cause logging output to go to the console.

Log4j – Frequently Asked Questions, xml (not log4j.xml) in the classpath. You can also specify the full path of the configuration file with  Move the log4j2.xml file so it's located in the default package. Move the log4j2.xml file anywhere in the project Name the log4j2.xml file as "log4j.xml" Create a folder in your project, place your log4j2.xml file there and add that folder to your runtime classpath


Where to place log4j.xml - log4j - iOS, 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  you can add it any where you want, when you run your project, configure the classpath and add the location of the log4j.properties files by clicking on: Run->Run Configuration -> [classpath tab] -> click on user Entries -> Advanced -> Select Add Folder -> select the location of your log4j.properties file and then -> OK -> run


log4j.xml Example, Where to put log4j.xml file? Vote Up21Vote Down Reply. 3 years ago. Hi, Where are we supposed to place log4j.xml? I get different logging (same log.xml contents!) depending on whether I put it in WEB-INF/classes in the WAR, the root of the EAR, META-INF/ of the EAR, lib/ of the EAR, or in the EJB JAR.


log4j.xml example configurations, We can configure log4j using both property file as well as xml file. Today we will look into log4j.xml example and get the details of log4j.xml configuration. In the above log4j.xml we have used RollingFileAppender which extends FileAppender to backup the log files when they reach defined size. The default maximum size of a file is 10MB. If you want to provide the max size, we can add below statement as param to RollingFileAppender.