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.
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
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.
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=
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.
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...!
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