4

I'm trying to configure the LogbackValve for getting access logs in case my Spring Boot based web application is running from embedded Tomcat. Following is the code for configuration:

import javax.servlet.Servlet;

import org.apache.catalina.startup.Tomcat;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import ch.qos.logback.access.tomcat.LogbackValve;

@Configuration
public class EmbeddedTomcatConfigurator {

    @Bean
    @ConditionalOnClass({ Servlet.class, Tomcat.class })
    @ConditionalOnBean(value = LogbackValve.class)
    public TomcatEmbeddedServletContainerFactory tomcatEmbeddedServletContainerFactory(LogbackValve logbackValve) {
        TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
        factory.addContextValves(logbackValve);
        return factory;
    }

    @Bean
    @ConditionalOnProperty(name = "embedded.tomcat.logback.access.config.path")
    public LogbackValve logbackValve(@Value("${embedded.tomcat.logback.access.config.path:}") String fileName) {
        LogbackValve logbackValve = new LogbackValve();
        logbackValve.setFilename(fileName);
        return logbackValve;
    }
}

However, everytime I start the application using "mvn spring-boot:run" in debug mode, I see logs saying, "LogbackValve not found" when trying to create instance of "tomcatEmbeddedServletContainerFactory" bean. However, another log statement indicates creation of this bean. Due to this, it always initializes the bean defined in the auto-configuration class "org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration".

For now, I've modified my class as :

import javax.servlet.Servlet;

import org.apache.catalina.startup.Tomcat;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import ch.qos.logback.access.tomcat.LogbackValve;

@Configuration
public class EmbeddedTomcatConfigurator {

    @Bean
    @ConditionalOnClass({ Servlet.class, Tomcat.class })
    @ConditionalOnProperty(name = "embedded.tomcat.logback.access.config.path")
    public TomcatEmbeddedServletContainerFactory tomcatEmbeddedServletContainerFactory(@Value("${embedded.tomcat.logback.access.config.path:}") String logbackAccessPath) {
        TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
        factory.addContextValves(getLogbackValve(logbackAccessPath));
        return factory;
    }

    private LogbackValve getLogbackValve(String fileName) {
        LogbackValve logbackValve = new LogbackValve();
        logbackValve.setFilename(fileName);
        return logbackValve;
    }

}

I've already asked this question on Git and it has been resolved. But, here, the point I'm trying to bring up is, why the @ConditionalOnBean(value = LogbackValve.class) isn't detecting the bean, which has been defined as well.

Shailesh
  • 61
  • 2
  • 8
  • 1
    To avoid wasting people's time, it would be courteous to mention that you've already [asked this question on GitHub and received an answer](https://github.com/spring-projects/spring-boot/issues/2937). If that answer doesn't meet you needs, you should explain why. – Andy Wilkinson May 12 '15 at 11:20
  • Thanks Andy. This is the first time, I've posted a comment on this forum. Sorry for any inconveniences. – Shailesh May 12 '15 at 13:53

0 Answers0