October 4, 2015

Spring Boot Tomcat logging configuration

I had a hard time in getting a SpringBoot Tomcat application configured in such a way that configuration files are read from TOMCAT_BASE/conf and log files are written to TOMCAT_BASE/logs. It should work from Eclipse as well as for an external Tomcat. Many questions and answers could be found about similiar problems, but this is what finally worked for me:

Put a bootstrap logback.xml into the classpath root and let point to the actual logging configuration:

            <include file="${catalina.base}/conf/myApp-logback.xml" />        


The logging configuration in ${catalina.base}/conf/myApp-logback.xml that writes to ${catalina.base}/logs/myApp.log looks something like this:      

            <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
                                   <pattern>%d{HH:mm:ss} [%thread] %-5level - %msg%n</pattern>

            <appender name="FILE" class="ch.qos.logback.core.FileAppender">
                                   <pattern>%d{yyyy.MM.dd HH:mm:ss} [%thread] %-5level - %msg%n</pattern>

            <root level="WARN">
                        <appender-ref ref="FILE" />
                        <appender-ref ref="STDOUT" />

            <logger name="de.myPackage" level="DEBUG"/>            

Other application settings are in a file at ${catalina.base}/conf/myApp.properties and configured like this:

@PropertySources(value = {@PropertySource("file:${catalina.base}/conf/myApp.properties")})
public class Application extends SpringBootServletInitializer {           

            public static void main(String[] args) {

                        SpringApplication.run(Application.class, args);


Finally, when running from Eclipse, catalina.base is defined as VM argument: