Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you tell Spring Boot to send the embedded Tomcat's access logs to stdout?

In a standalone Spring Boot web application (executable jar), how do you tell Spring Boot that we want the embedded Tomcat instance's HTTP access logs to be sent to stdout?

like image 855
Les Hazlewood Avatar asked Apr 21 '16 21:04

Les Hazlewood


2 Answers

Update 2019.02.11:

These inks should be useful to map which properties you should set in application.properties:

  • tomcat
  • undertow

@acohen answer is slightly correct. If you provide the empty double quotes it won't work. I will extend his answer because I think it's important for people who don't want to mess with adding dependencies or modifying code:

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=

Notes

  1. If you set file-date-format and suffix to be double quotes, you will have this error:
java.io.FileNotFoundException: /dev/stdout"""" (Permission denied)

  1. If you don't include them in the config file, you will then be using defaults values and this error:
java.io.FileNotFoundException: /dev/stdout.2019-02-07.log (Permission denied)
  1. If you leave them empty, then it will work.
like image 147
Sebastian Avatar answered Oct 19 '22 09:10

Sebastian


If you use Logback, you can use logback-access for this.

Add dependency ch.qos.logback:logback-access

Optional Javaconfig to add TeeFilter (request & response logging):

@Bean(name = "TeeFilter")
public Filter teeFilter() {
    return new ch.qos.logback.access.servlet.TeeFilter();
}

Javaconfig for embedded tomcat:

@Bean
public EmbeddedServletContainerFactory servletContainer() {
    TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();

    // put logback-access.xml in src/main/resources/conf
    tomcat.addContextValves(new LogbackValve());

    return tomcat;
}

Contents for logback-access.xml (save in 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>
like image 32
JohanB Avatar answered Oct 19 '22 10:10

JohanB