0

I have a web-API that deploys on a tomcat. I want to use log4j2 to print from the the API so I can track the calls that made to the API.

The war-file will be deployed on different machines with tomcat. I want to be able to place the log4j.properties file in a place where I don't overwrite it each time I make a new deploy.

As I've understood it the log4j.properties files should be placed in src/main/rescources when using maven (I am using maven). When I place the file in that folder I get log4j2 working. However if I make a new deploy the log4j.properties-file will be overwritten.

Is there anyway to place the log4j.propterties file outside that folder when I use Maven? Perhaps in the tomcat folder?

I've tried to make changes in the pom.xml. I tried to change the location in two places.

<log4j.configuration>./conf/log4j.properties</log4j.configuration>

And

    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <targetPath>${project.build.directory}</targetPath>
            <includes>
                <include>log4j.properties</include>
            </includes>
        </resource>
    </resources>

I tried just changing the location in one of these at a time and in both of them in the same time. I tried to put the log4j.properties in the tomcat catalog.

I don't know if this is possible to do when you're using maven. Anyone who knows and can help?

  • The file whould be packaged into the war file you are using so how could that file being overwritten ? And why do you change the resource configuration ? Keep the conventions.... – khmarbaise Jun 27 '17 at 11:14
  • what do you mean by - log4j.properties-file will be overwritten? – Vishal Roy Jun 27 '17 at 11:19
  • https://stackoverflow.com/questions/7390521/change-location-of-log4j-properties – Stefan Jun 27 '17 at 13:16
  • What I mean with overwritten is that my log4j.properties file after a deploy is in the folder C:\Program Files\Apache Software Foundation\Tomcat 8.5\webapps\myWebap\WEB-INF\classes. If I would change the log4j.properties in this folder, and then later on would make a change in my code and make a new deploy the log4j.properties would be replaced with the version that is in eclipse. I might want to have different log4j.properties files in different machines. – Pernilladwl Jun 29 '17 at 06:21

2 Answers2

2

From the Log4j 2 documentation:

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.
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 or log4j2-test.yml in the classpath.
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.
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 or log4j2.yml on the classpath.
If a YAML file cannot be located the JSON ConfigurationFactory will look for log4j2.json or log4j2.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.

So you should be able to set the "log4j.configurationFile" property when you start tomcat to the location of your config file.

Henry
  • 42,982
  • 7
  • 68
  • 84
0

This is a common problem with regard to log4j/logback/etc

If you are using 'log4j.properties'. You can specify a -D (via a setenv.bat/sh file in %CATALINA_BASE%\bin or ${CATALINA_BASE}/bin respectively).

See this answer for some more detail on the format.

However, with log4j2 you can use the XInclude syntax if you switch to an XML format. By doing so, you can place your configuration in say ${catalina.base}/conf/log4j2-included.xml and your embedded log4j2 configuration would include that file.

Dave G
  • 9,639
  • 36
  • 41
  • How do I use log4j2 as xml? I've tried to use it as XML-format instead of a log4j.properties. However I don't get any log only the following error: log4j:WARN No appenders could be found for logger (com.cgi.searcheditor.jerseyjackson.jaxrs.resource.MyClass). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. – Pernilladwl Jun 29 '17 at 10:44