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:
<configuration>

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

</configuration>

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

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

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

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

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

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:
-Dcatalina.base=.

No comments:

Post a Comment