如何在spring-boot中配置tomcat访问日志的位置和名称? [英] How do I configure the location and name of tomcat access log in spring-boot?

查看:983
本文介绍了如何在spring-boot中配置tomcat访问日志的位置和名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在application.yml中有一个具有以下配置的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

这会提示创建访问日志tomcat/logs/access_log.2015-02-12.txt.

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?

使用logback和在logback.xml中进行配置,应用程序日志记录可以正常工作

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

推荐答案

您可以使用EmbeddedServletContainerCustomizer接口将完全自定义的阀门添加到嵌入式tomcat.这是对我有用的东西:

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

}

这篇关于如何在spring-boot中配置tomcat访问日志的位置和名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆