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

时间:2016-04-21 21:18:26

标签: tomcat logging spring-boot stdout

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

5 个答案:

答案 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)

更新2019.02.11:

这些墨水应有助于映射应在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. 如果您将它们留空,那么它将起作用。

答案 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