My 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文件,其中包含正确的访问日志,但在我的concole上注意到,似乎配置文件logback-access.xml没有被读取。
知道吗?
埃里克
我弄错了还是 Spring Boot 本身不支持?
资料来源: https://github.com/spring-projects/spring-boot/issues/2609:
嘿,我正在尝试获得回点访问 + 雄猫与 spring 一起工作 靴子。有没有人能够开箱即用?或者是 有一些必要的管道需要设置吗?
。
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>
编辑 - 其他日志解决方案 1.1.6+
关于上面提到的弹簧启动问题,有人发布了这个:
从 logback 1.1.6 开始,无需任何解决方法即可将 logback 访问配置文件作为资源加载。参考: http://jira.qos.ch/browse/LOGBACK-1069
您所要做的就是:
logbackValve.setFilename("log-access.xml");
虽然我迟到了,为后代发布了我的简单工作代码。
logback
的 ACCESS 日志可以通过以下步骤打印在控制台日志中:
- 添加
logback-access
依赖项
implementation group: 'ch.qos.logback', name: 'logback-access', version: '1.2.3'
- 注入
TomcatServletWebServerFactory
添加的实例值LogbackValve
。
@Bean
public TomcatServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
tomcat.addContextValves(new LogbackValve());
return tomcat;
}
- 将以下
logback-access.xml
添加到类路径resourcesconf
目录中。
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<Pattern>combined</Pattern>
<Pattern>[ACCESS] %h %l %u %t{yyyy-MM-dd HH:mm:ss.SSS} %s %b %D ms</Pattern>
</encoder>
</appender>
<appender-ref ref="STDOUT" />
</configuration>
访问日志将在控制台中打印为
"[ACCESS] <host> <date> "<httpmethod> <httpuri> HTTP/1.1" <httpstatus> "<timetaken in millisecond> ms""