Search code examples
tomcatspring-bootlogback

How do I configure the location and name of tomcat access log in spring-boot?


I have a spring-boot app with the following configuration in application.yml

server:
contextPath: /rti
tomcat:
    access-log-enabled: true
    access-log-pattern: "%h %l %u %t \"%r\" %s %b %D"
    basedir: tomcat

This prompts the creation of an access log tomcat/logs/access_log.2015-02-12.txt.

I would like to be able to configure where the access log is created and what it is named; but after much searching I am starting to think this isn't possible. Does any one know how to achieve this?

Application logging is working fine using logback and configuration in logback.xml


Solution

  • You can use the EmbeddedServletContainerCustomizer interface to add a completely custom valve to your embedded tomcat. Here is what works for me:

    @Configuration
    public class WebConfig extends WebMvcConfigurerAdapter implements EmbeddedServletContainerCustomizer {
    
        @Override
        public void customize(ConfigurableEmbeddedServletContainer container) {
            if (container instanceof TomcatEmbeddedServletContainerFactory) {
                TomcatEmbeddedServletContainerFactory factory = (TomcatEmbeddedServletContainerFactory) container;
                AccessLogValve accessLogValve = new AccessLogValve();
                accessLogValve.setDirectory("/var/log/test");
                accessLogValve.setPattern("common");
                accessLogValve.setSuffix(".log");
                factory.addContextValves(accessLogValve);
            } else {
                logger.error("WARNING! this customizer does not support your configured container");
            }
        }
    
    }