Where to place logback.xml in tomcat Docker container

I introduce how to reflect logback.xml to deploy a war app inside a tomcat Docker container.


I usually put logback.xml directly under src/main/resources (on the classpath) when I’m developing java applications in Eclipse. But where should I put it outside of the development environment?


The place to locate logback.xml

You can find the rules here.


  1. Logback tries to find a file called logback-test.xml in the classpath.
  2. If no such file is found, logback tries to find a file called logback.groovy in the classpath.
  3. If no such file is found, it checks for the file logback.xml in the classpath..
  4. If no such file is found, service-provider loading facility (introduced in JDK 1.6) is used to resolve the implementation of com.qos.logback.classic.spi.Configurator interface by looking up the file META-INF\services\ch.qos.logback.classic.spi.Configurator in the class path. Its contents should specify the fully qualified class name of the desired Configurator implementation.
  5. If none of the above succeeds, logback configures itself automatically using the BasicConfigurator which will cause logging output to be directed to the console.

During development 1~3 is enough, output to the Eclipse console or output to a file etc.
By using point 4, in the staging and production environments, we can use this method to set the location of the logback.xml.
It’s easy! Just specify the following as a VM runtime argument.
Even if you have a logback.xml in your classpath, you could confirm that the VM runtime argument takes precedence over classpath.

java -Dlogback.configurationFile=/usr/local/tomcat/conf/logback.xml

Apply Docker tomcat container

Now, I was trying to deploy and run war in a Docker tomcat container. This is how I would pass the location of the logback.xml.

The Docker container image I used is as follows. https://hub.docker.com/_/tomcat

You can use setenv.sh to set VM runtime arguments to tomcat.
If you place the setenv.sh file under bin directory, catalina.sh will load it automatically.
catalina.sh is the file that will be executed when the above tomcat container is started.

This is a simple setenv.sh file.


JAVA_OPTS="-server \
           -Dfile.encoding=UTF-8 \

Use docker run command to place it directly under bin directory in CATALINA_HOME.
And mount the logback.xml and war in the container.

docker run -p 8080:8080 -d \
   -v /path/to/setenv.sh:/usr/local/tomcat/bin/setnev.sh \
   -v /path/to/logback.xml:/usr/local/tomcat/conf/logback.xml \
   -v /path/to/sample.war:/usr/local/tomcat/webapps/sample.war \

You can pass a logback.xml file to tomcat.

Check the loaded logback.xml

This is for your reference in case it doesn’t work.

To check the loading status of logback.xml, you can add the statusListener tag directly under the configuration tag in logback.xml as shown below.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration>
  <statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener" />

Then the log will tell you how to look for logback.xml.

14:44:54,034 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
14:44:54,035 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
14:44:54,035 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/home/user/Develop/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/sample-front/WEB-INF/classes/logback.xml]

For more information, see Startus Listener.