简体   繁体   中英

Spring Boot, Tomcat Access Log with Logback config file in a packaged application

How do I properly configure my LogbackValve in the EmbeddedServletContainerCustomizer-Bean to read a File on the class path?

Right now I have the following Configuration in place, which works perfectly on my localhost ( logging.accessLogConfig: src/main/resources/logback_access_dev.xml is set in application.yml):

@Configuration
public class TomcatConfiguration {

@Value("${logging.accessLogConfig}")
private String accessLogConfig;

@Bean
public EmbeddedServletContainerCustomizer containerCustomizer() {
    return new EmbeddedServletContainerCustomizer() {
        @Override
        public void customize(ConfigurableEmbeddedServletContainer container) {
            if (container instanceof TomcatEmbeddedServletContainerFactory) {
                TomcatEmbeddedServletContainerFactory containerFactory =
                        (TomcatEmbeddedServletContainerFactory) container;

                LogbackValve logbackValve = new LogbackValve();
                logbackValve.setFilename(accessLogConfig);

                containerFactory.addContextValves(logbackValve);
            }
        }
    };
}

}

But as soon as I deploy this on my Server as aa packaged application, the file can't be found anymore: -WARN in ch.qos.logback.access.tomcat.LogbackValve[] - [src/main/resources/logback_access_dev.xml] does not exist

I was playing around as well with changing logging.accesslogConfig: classpath:logbook_access_dev.xml , and reading the content to a temporary file, but that didn't work either:

@Configuration
public class TomcatConfiguration {

private static final Logger logger = LoggerFactory.getLogger(TomcatConfiguration.class);

@Value("${logging.accessLogConfig}")
private Resource accessLogConfig;

@Bean
public EmbeddedServletContainerCustomizer containerCustomizer() {
    return new EmbeddedServletContainerCustomizer() {
        @Override
        public void customize(ConfigurableEmbeddedServletContainer container) {
            if (container instanceof TomcatEmbeddedServletContainerFactory) {
                TomcatEmbeddedServletContainerFactory containerFactory =
                        (TomcatEmbeddedServletContainerFactory) container;

                try {
                    InputStream configuration = accessLogConfig.getInputStream();
                    File configFile = File.createTempFile(accessLogConfig.getFilename(), "tmp");
                    FileUtils.copyInputStreamToFile(configuration, configFile);

                    LogbackValve logbackValve = new LogbackValve();
                    logbackValve.setFilename(configFile.getAbsolutePath());

                    containerFactory.addContextValves(logbackValve);
                }
                catch (IOException e) {
                    logger.warn("could not read access log configuration {}", accessLogConfig);
                }
            }
        }
    };
}

}

I'm using Spring Boot 1.2.7.RELEASE together with logback-access 1.1.3.

Any help on how to get that running is highly appreciated :)

solved the problem now with creating my own SpringLogbackValve which accepts an InputStream instead of a filename. it's basically a clone of ch.qos.logback.access.tomcat.LogbackValve, just with an Inputstream inputStream instead of String fileName and the following method:

@Override
public void startInternal() throws LifecycleException {
    executorService = ExecutorServiceUtil.newExecutorService();

    if (inputStream != null) {
        try {
            JoranConfigurator jc = new JoranConfigurator();
            jc.setContext(this);
            jc.doConfigure(inputStream);
        }
        catch (JoranException e) {
            logger.warn("failed to configure {}", inputStream);
        }
    }
    else {
        getStatusManager().add(
                new WarnStatus("[" + inputStream + "] does not exist", this));
    }

    if (!quiet) {
        StatusPrinter.print(getStatusManager());
    }

    started = true;
    setState(LifecycleState.STARTING);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM