如何使用Spring Boot配置logback-access.xml

Eri*_*son 8 java spring logback spring-boot

我的application.yml是:

server:
  tomcat:
    accesslog:
      enabled: true
    basedir: my-tomcat
Run Code Online (Sandbox Code Playgroud)

我们使用spring boot 1.4.3.RELEASE,我想配置一个logback-access.xml(在src/main/resources下),内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <!-- always a good activate OnConsoleStatusListener -->
  <statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener" />

  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%h %l %u %user %date "%r" %s %b</pattern>
    </encoder>
  </appender>

  <appender-ref ref="STDOUT" />
</configuration>
Run Code Online (Sandbox Code Playgroud)

我可以在my-tomcat文件夹下看到一个access_log.2017-01-03.log文件,其中包含正确的访问日志但是在我的concole上注意到,似乎没有读取配置文件logback-access.xml.

任何的想法 ?

埃里克

ale*_*xbt 5

我错了还是 Spring Boot 本身不支持这个?

来源:https : //github.com/spring-projects/spring-boot/issues/2609

嘿,我正在尝试让 logback-access + tomcat 与 spring boot 一起工作。有没有人能够让这个工作开箱即用?或者是否需要设置一些必要的管道?

...

As a workaround, you can copy the access xml from the class path to the filesystem and run it there as part of your configuration class

Files.copy(this.getClass().getResourceAsStream("/logback-access.xml"),Paths.get("log-access.xml"),StandardCopyOption.REPLACE_EXISTING);
logbackValve.setFilename("log-access.xml");
Run Code Online (Sandbox Code Playgroud)

解决方案

使用spring-boot-ext-logback-access

只需添加依赖项即可:

<dependency>
    <groupId>net.rakugakibox.spring.boot</groupId>
    <artifactId>logback-access-spring-boot-starter</artifactId>
    <version>2.7.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

编辑 - logback 1.1.6+ 的其他解决方案

上面提到spring-boot 问题上,有人发布了这个:

从 logback 1.1.6 开始,不需要任何解决方法来加载 logback-access 配置文件作为资源。参考:http : //jira.qos.ch/browse/LOGBACK-1069

您所要做的就是: logbackValve.setFilename("log-access.xml");