简体   繁体   English

Spring Boot,打包应用程序中带有Logback配置文件的Tomcat访问日志

[英]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? 如何在EmbeddedServletContainerCustomizer-Bean中正确配置LogbackValve来读取类路径上的文件?

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): 现在,我具有以下配置,该配置在我的本地主机上可以正常运行( logging.accessLogConfig: src/main/resources/logback_access_dev.xml在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 但是,一旦我将其作为打包的应用程序部署到服务器上,便不再能够找到该文件: -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: 我也正在通过更改logging.accesslogConfig: classpath:logbook_access_dev.xml并将内容读取到一个临时文件中来解决问题,但这也不起作用:

@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. 我正在使用Spring Boot 1.2.7.RELEASE以及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. 现在通过创建自己的SpringLogbackValve解决了该问题,该SpringLogbackValve接受InputStream而不是文件名。 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: 它基本上是ch.qos.logback.access.tomcat.LogbackValve的副本,只是一个Inputstream Inputstream inputStream而不是String fileName和以下方法:

@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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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