gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:54:17 30 4
gpt4 key购买 nike

我的 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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com