[JAVA] Output embedded Tomcat access log to standard output with Spring Boot

Overview

The built-in Tomcat of Spring Boot can automatically output the access log to a file. This article will show you how to output this to standard output.

environment

First, output the access log

Write a RestController that receives a nice GET and POST.

@RequestMapping("hello")
@RestController
public class HelloController {

    @GetMapping("")
    public String hello() {
        return "Hello, world!";
    }

    @PostMapping("")
    public String message(@RequestBody String message) {
        return "Hello, " + message + "!";
    }
}

You can output the access log by describing the following settings in application.properties.

server.tomcat.accesslog.enabled=true
server.tomcat.basedir=/path/to/dir
server.tomcat.accesslog.directory=logs
server.tomcat.accesslog.suffix=.log
server.tomcat.accesslog.prefix=access_log
server.tomcat.accesslog.file-date-format=.yyyy-MM-dd

When I actually hit it locally, the access log was output to /path/to/dir/logs/access_log.yyyy-MM-dd.log.

$ curl localhost:8080/hello
Hello, world!
curl -H "Content-Type: text/plain" localhost:8080/hello -d "mito"
Hello, mito!
$ ls /path/to/dir/logs
access_log.2019-11-08.log
$ cat /path/to/dir/logs/access_log.2019-11-08.log
0:0:0:0:0:0:0:1 - - [08/Nov/2019:17:59:56 +0900] "GET /hello HTTP/1.1" 200 13
0:0:0:0:0:0:0:1 - - [08/Nov/2019:18:00:01 +0900] "POST /hello HTTP/1.1" 200 16

Output access log to standard output

As also mentioned in the Twelve-Factor App, it is more common for modern applications to output logs to standard output.

There are several ways to output to standard output.

Output to / dev / stdout

You can output to standard output by devising the settings and setting the output destination file to / dev / stdout.

server.tomcat.accesslog.enabled=true
server.tomcat.basedir=/dev
server.tomcat.accesslog.directory=stdout
server.tomcat.accesslog.suffix=
server.tomcat.accesslog.prefix=
server.tomcat.accesslog.file-date-format=

Use logback-access

In some cases, you may want to avoid touching / dev / stdout directly. You can set the output destination to standard output by using logback-access.

First, place logback-access.xml in src / main / resources / conf /. Describe logback-access.xml as follows so that it is output to stdout.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <!--  combined log format-->
      <pattern>combined</pattern>
    </encoder>
  </appender>

  <appender-ref ref="STDOUT" />

</configuration>

Add more logback-access dependency (below for gradle)

dependencies {
  ...
+	implementation group: 'ch.qos.logback', name: 'logback-access', version: '1.2.3'
}

By creating a Bean of TomcatServletWebSErverFactory, the settings are reflected in the embedded tomcat.

@Configuration
public class TomcatLoggingConfig {
    @Bean
    public TomcatServletWebServerFactory servletContainer() {
        TomcatServletWebServerFactory tomcatServletWebServerFactory = new TomcatServletWebServerFactory();
        //LogbackValve refers to resources and below, so this is logback-access.The contents of xml are reflected
        tomcatServletWebServerFactory.addContextValves(new LogbackValve());
        return tomcatServletWebServerFactory;
    }
}

When you start it and actually make a request, you can see that the following log is output.

0:0:0:0:0:0:0:1 - - [08/11/2019:18:38:29 +0900] "GET /hello HTTP/1.1" 200 13 "-" "curl/7.54.0"
0:0:0:0:0:0:0:1 - - [08/11/2019:18:38:20 +0900] "POST /hello HTTP/1.1" 200 16 "-" "curl/7.54.0"

There is no need to set application.properties.

Use logback-access-spring-boot-starter

By using logback-access-spring-boot-starter, the above logback-access settings are automatically reflected. Will do it.

Add a dependency,

dependencies {
  ...
+ implementation group: 'net.rakugakibox.spring.boot', name: 'logback-access-spring-boot-starter', version: '2.7.1'
}

If you place logback-access.xml directly under src / main / resources, the access log will be output to the standard output. Convenient...!

reference

https://www.baeldung.com/spring-boot-embedded-tomcat-logs http://logback.qos.ch/access.html https://stackoverflow.com/questions/36780680/how-do-you-tell-spring-boot-to-send-the-embedded-tomcats-access-logs-to-stdout?answertab=votes#tab-top https://github.com/akihyro/logback-access-spring-boot-starter

Recommended Posts

Output embedded Tomcat access log to standard output with Spring Boot
Flow until output table data to view with Spring Boot
Output Spring Boot log in json format
How to use MyBatis2 (iBatis) with Spring Boot 1.4 (Spring 4)
How to use built-in h2db with spring boot
Output request and response log in Spring Boot
Try to implement login function with Spring Boot
Try to automate migration with Spring Boot Flyway
[Java] Article to add validation with Spring Boot 2.3.1.
I wanted to gradle spring boot with multi-project
Replaced Tomcat 8.5 log output with Log4j2.8 or later
[Introduction to Spring Boot] Authentication function with Spring Security
Download with Spring Boot
Settings for connecting to MySQL with Spring Boot + Spring JDBC
Automatically map DTOs to entities with Spring Boot API
Access the built-in h2db of spring boot with jdbcTemplate
Output log to external file with slf4j + logback with Maven
How to output standard from an array with forEach
Deploy the Spring Boot project to Tomcat on XAMPP
How to boot by environment with Spring Boot of Maven
Attempt to SSR Vue.js with Spring Boot and GraalJS
Try Spring Boot from 0 to 100.
Generate barcode with Spring Boot
Hello World with Spring Boot
Implement GraphQL with Spring Boot
Get started with Spring boot
Hello World with Spring Boot!
Extract SQL to property file with jdbcTemplate of spring boot
Run LIFF with Spring Boot
SNS login with Spring Boot
Introduction to Spring Boot ① ~ DI ~
File upload with Spring Boot
Spring Boot starting with copy
Until INSERT and SELECT to Postgres with Spring boot and thymeleaf
Control log output with Doma2
Introduction to Spring Boot ② ~ AOP ~
Connect to database with spring boot + spring jpa and CRUD operation
Spring Boot starting with Docker
Java standard log output sample
Hello World with Spring Boot
Set cookies with Spring Boot
Use Spring JDBC with Spring Boot
Add module with Spring Boot
Getting Started with Spring Boot
Introduction to Spring Boot Part 1
Create microservices with Spring Boot
Send email with spring boot
I tried to get started with Swagger using Spring Boot
[Java] I want to test standard input & standard output with JUnit
What to do if the log using JUL is no longer output to the app log after deploying the Spring Boot app to Tomcat as a war
Introduction to Spring Boot x OpenAPI ~ OpenAPI made with Generation gap pattern ~
How to create your own Controller corresponding to / error with Spring Boot
Sample code to unit test a Spring Boot controller with MockMvc
Use Basic Authentication with Spring Boot
gRPC on Spring Boot with grpc-spring-boot-starter
Create an app with Spring Boot 2
Hot deploy with Spring Boot development
Database linkage with doma2 (Spring boot)
How to set Spring Boot + PostgreSQL
Spring Boot programming with VS Code
Until "Hello World" with Spring Boot