Spring Boot 如何告诉Sping Boot 将嵌入的Tomcat的访问日志发送到stdout?

u5rb5r59  于 2023-04-11  发布在  Spring
关注(0)|答案(5)|浏览(163)

在一个独立的Sping Boot Web应用程序(可执行jar)中,如何告诉Spring Boot我们希望将嵌入式Tomcat示例的HTTP访问日志发送到stdout?

x8goxv8g

x8goxv8g1#

更新2019.02.11:

这些墨迹应该有助于Map您应该在application.properties中设置的属性:

@acohen的回答稍微正确一点。如果你提供了空的双引号,它将不起作用。我将扩展他的回答,因为我认为这对那些不想添加依赖项或修改代码的人来说很重要:

config/application.properties

# 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=

注意事项

1.如果将file-date-formatsuffix设置为双引号,则会出现以下错误:

java.io.FileNotFoundException: /dev/stdout"""" (Permission denied)

1.如果你没有在配置文件中包含它们,你将使用默认值,并出现以下错误:

java.io.FileNotFoundException: /dev/stdout.2019-02-07.log (Permission denied)

1.如果你让它们空着,它就会工作。

trnvg8h3

trnvg8h32#

如果使用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>
i2loujxw

i2loujxw3#

我在Sping 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=""
v9tzhpje

v9tzhpje4#

以下是JohanB对Sping Boot 2.0.0+的精彩回答的后续内容。
在Sping 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;
}
8aqjt8rx

8aqjt8rx5#

JohanB的解决方案是可行的,但如果你不想写代码,有人做得更好, Package 了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>

在classpath根目录下有一个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

相关问题