在一个独立的Spring Boot Web应用程序(可执行jar)中,您如何告诉Spring Boot我们希望将嵌入式Tomcat实例的HTTP访问日志发送到stdout?
答案 0 :(得分:10)
如果您使用Logback,则可以使用logback-access。
添加依赖项ch.qos.logback:logback-access
可选的Javaconfig添加TeeFilter(请求和响应日志记录):
@Bean(name = "TeeFilter")
public Filter teeFilter() {
return new ch.qos.logback.access.servlet.TeeFilter();
}
嵌入式tomcat的Javaconfig:
@Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
// put logback-access.xml in src/main/resources/conf
tomcat.addContextValves(new LogbackValve());
return tomcat;
}
logback-access.xml
的内容(保存在src/main/resources/conf
)
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<Pattern>combined</Pattern>
<Pattern>%fullRequest%n%n%fullResponse</Pattern>
</encoder>
</appender>
<appender-ref ref="STDOUT" />
</configuration>
答案 1 :(得分:5)
这些墨水应有助于映射应在application.properties
中设置的属性:
@acohen答案是正确的。如果提供空的双引号将不起作用。我将扩展他的答案,因为我认为这对于不想惹麻烦添加依赖项或修改代码的人很重要:
# here we say that we want to enable accesslog
server.tomcat.accesslog.enabled=true
# it is important to understand what the options means:
# 'directory/prefix + suffix + file-date-format' will be
# the file that tomcat will try to open.
# /dev/stdout is standard output, so we want tomcat
# to write to that fd. Then, we need to play with
# directory, prefix, suffix and file-date-format to match our desired path.
server.tomcat.accesslog.directory=/dev
server.tomcat.accesslog.prefix=stdout
server.tomcat.accesslog.buffered=false
# Don't use empty double quotes, see below
server.tomcat.accesslog.suffix=
server.tomcat.accesslog.file-date-format=
file-date-format
和suffix
设置为双引号,则会出现此错误:java.io.FileNotFoundException: /dev/stdout"""" (Permission denied)
java.io.FileNotFoundException: /dev/stdout.2019-02-07.log (Permission denied)
答案 2 :(得分:4)
这是在Spring Boot 2.x上为我完成的:
server.tomcat.accesslog.enabled=true
server.tomcat.accesslog.directory=/dev
server.tomcat.accesslog.prefix=stdout
server.tomcat.accesslog.buffered=false
server.tomcat.accesslog.suffix=""
server.tomcat.accesslog.file-date-format=""
答案 3 :(得分:1)
以下是JohanB对Spring Boot 2.0.0+做出的一个很好的回答。
在Spring Boot 2.0.0中,EmbeddedServletContainerFactory
被替换为TomcatServletWebServerFactory
。 JohanB答案的所有其他方面仍然可以正常运行,只需修改工厂bean的创建即可:
@Bean
public TomcatServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
// put logback-access.xml in src/main/resources/conf
tomcat.addContextValves(new LogbackValve());
return tomcat;
}
答案 4 :(得分:0)
JohanB的解决方案有效,但是如果您不想编写代码,则有人会做得更好,并包装Server access logs in a nice Spring Boot starter。它涵盖了Tomcat,Jetty和Undertow。
只需添加依赖项:
<dependency>
<groupId>net.rakugakibox.spring.boot</groupId>
<artifactId>logback-access-spring-boot-starter</artifactId>
<version>2.7.1</version>
</dependency>
在类路径根目录下还有一个logback-access.xml文件:
<configuration>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>common</pattern>
</encoder>
</appender>
<appender-ref ref="CONSOLE" />
</configuration>
,访问日志将打印到stdout:
127.0.0.1 - - [08/févr./2019:11:23:30 +0100] "GET /password HTTP/1.1" 200 32
这时,如果要打印完整的HTTP请求和响应以进行调试,则需要create the TeeFilter
Bean on your own。