Search code examples
javatomcatspring-bootloggingtomcat8

Specify location of external logging configuration to specific webapps using Tomcat 8


I'm restricted to use Tomcat 8 and I need to deploy several spring boot based web applications on the same Tomcat.

I'm trying to avoid building the artifact with it's own logging configuration included inside the war file.

By using the system property -Dlogging.conf it's possible to specify the logback.xml file for one application. Adding -Dlogging.conf in setenv.sh would however point the same logging configuration for every webapp deployed in Tomcat.

Is there a smart way to handle external logging configurations using Tomcat?


Solution

  • Specifying external logging configurations can be done using context.

    There are several ways of defining contexts. A good summary is given in the following answere: https://stackoverflow.com/a/26126563/809043

    I'm now saving a context file for each webapp under:

    .../tomcat/conf/Catalina/localhost/APP-NAME.xml 
    

    where app-name is the deployed name of the web application.

    The context point outs a resource folder containing the logback.xml configuration. The specified resource folder is then made accessible to the webbapp as if it where located in /WEB-INF/classes/

    The below xml shows an example of a context file which can be used to include external logging and other configurations needed by a webbapp.

    <?xml version="1.0" encoding="UTF-8"?>
    
    <Context path="/APP-NAME">
        <Resources className="org.apache.catalina.webresources.StandardRoot">
            <PreResources  className="org.apache.catalina.webresources.DirResourceSet"
                base="/path/to/logging/config/folder"
                internalPath="/"
                webAppMount="/WEB-INF/classes" />
        </Resources>
    </Context>