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

标签 java spring spring-boot logback

我的 application.yml 是:

server:
  tomcat:
    accesslog:
      enabled: true
    basedir: my-tomcat

我们使用 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>

我可以在 my-tomcat 文件夹下看到 access_log.2017-01-03.log 文件,其中包含正确的访问日志,但在我的控制台上注意到,配置文件 logback-access.xml 似乎未被读取。

有什么想法吗?

埃里克

最佳答案

是我弄错了还是 Spring Boot 本身不支持它?

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

Hey, I'm trying to get logback-access + tomcat working with spring boot. Has anyone been able to get this working out-of-the-box? Or is there some necessary plumbing to set up?

...

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

解决方案

使用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>

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

关于 spring-boot issue mentioned above ,有人发布了这个:

Since logback 1.1.6 there is no need of any workarounds in order to load the logback-access configuration file as a resource. Reference: http://jira.qos.ch/browse/LOGBACK-1069

All you have to do is: logbackValve.setFilename("log-access.xml");

关于java - 如何使用 Spring Boot 配置 logback-access.xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41448491/

相关文章:

java - 我真的需要 Java 来编译 Java 吗?

java - 编辑时从 Spring @Valid 中删除一些类属性

java - Spring JUnit4 手动/自动布线困境

hibernate - JOIN FETCH 查询未在 Spring DataJpaTest 中获取惰性集合

java - Spring JPA Hibernate 自动填充审计字段(创建 ID/时间戳等)

java - 在 JSON 字符串中添加双引号的简单方法

java - 当我尝试在线程内访问静态方法时,它会抛出 NullPointerException

java - GWT(P) 中的包组织

java - 在同一个项目中使用 spring mvc 应用程序和 jersey 是否存在潜在问题?

spring boot 执行器为执行器端点返回 401