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.
Introduction
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.
https://logback.qos.ch/manual/configuration.html#auto_configuration
- Logback tries to find a file called logback-test.xml in the classpath.
- If no such file is found, logback tries to find a file called logback.groovy in the classpath.
- If no such file is found, it checks for the file logback.xml in the classpath..
- 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.
- 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.
Use docker run command to place it directly under bin directory in CATALINA_HOME.
And mount the logback.xml and war in the container.
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.
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.