Spring Boot, Log4J2, and Tomcat logging

In Spring Boot you can enable Tomcat logging by declaring relevant loggers and log levels in application property files:

Listing 1. application.yaml
logging.level.org.apache.tomcat: DEBUG
logging.level.org.apache.catalina: DEBUG

However, these declarations alone won’t work if you switch to Log4J2 for logging [2].

Fortunately, there’s an easy way to fix this issue by setting a system property:

Listing 2. java.util.logging.manager
-Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager

It is also possible to turn on request logging using application property files:

server:
  tomcat:
    accesslog:
      enabled: true

By default, access logs are stored under a randomly generated Tomcat base directory. For example:

/tmp/tomcat.6716050071320465152.8080/
├── work
│   └── Tomcat
│       └── localhost
│           └── ROOT
└── logs
    └── access_log.2019-10-25.log

Here’s a sample access log entry:

Listing 3. Access Log
127.0.0.1 - - [26/Oct/2019:09:57:27 +1100] "GET /demo?xyz=1&abc=2 HTTP/1.1" 404 136

Finally, for a more verbose request logging, you can enable the request dumper filter:

Listing 4. Enabling RequestDumperFilter
@Bean
public FilterRegistrationBean requestDumperFilter() {
    final var registration = new FilterRegistrationBean<RequestDumperFilter>();
    final var requestDumperFilter = new RequestDumperFilter();

    registration.setFilter(requestDumperFilter);
    registration.addUrlPatterns("/*");

    return registration;
}

Here’s an example output produced by this configuration:

Listing 5. Sample log
 Counting up[http-nio-8080-Acceptor] latch=1
 Socket: [org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper@22e73315:org.apache.tomcat.util.net.NioChannel@756305f3:java.nio.channels.SocketChannel[connected local=/127.0.0.1:8080 remote=/127.0.0.1:54862]], Read from buffer: [0]
 Security checking request GET /demo
   No applicable constraints defined
  Not subject to any constraint
 http-nio-8080-exec-3 START TIME        =26-Oct-2019 09:57:27
 http-nio-8080-exec-3         requestURI=/demo
 http-nio-8080-exec-3           authType=null
 http-nio-8080-exec-3  characterEncoding=UTF-8
 http-nio-8080-exec-3      contentLength=-1
 http-nio-8080-exec-3        contentType=null
 http-nio-8080-exec-3        contextPath=
 http-nio-8080-exec-3             header=foo=1
 http-nio-8080-exec-3             header=bar=2
 http-nio-8080-exec-3             header=host=localhost:8080
 http-nio-8080-exec-3             header=connection=Keep-Alive
 http-nio-8080-exec-3             header=user-agent=Apache-HttpClient/4.5.9 (Java/11.0.4)
 http-nio-8080-exec-3             header=accept-encoding=gzip,deflate
 http-nio-8080-exec-3             locale=en_AU
 http-nio-8080-exec-3             method=GET
 Set encoding to UTF-8
 Decoding query null UTF-8
 Start processing with input [xyz=1&abc=2]
 http-nio-8080-exec-3          parameter=xyz=1
 http-nio-8080-exec-3          parameter=abc=2
 http-nio-8080-exec-3           pathInfo=null
 http-nio-8080-exec-3           protocol=HTTP/1.1
 http-nio-8080-exec-3        queryString=xyz=1&abc=2
 http-nio-8080-exec-3         remoteAddr=127.0.0.1
 http-nio-8080-exec-3         remoteHost=127.0.0.1
 http-nio-8080-exec-3         remoteUser=null
 http-nio-8080-exec-3 requestedSessionId=null
 http-nio-8080-exec-3             scheme=http
 http-nio-8080-exec-3         serverName=localhost
 http-nio-8080-exec-3         serverPort=8080
 http-nio-8080-exec-3        servletPath=/demo
 http-nio-8080-exec-3           isSecure=false
 http-nio-8080-exec-3 ------------------=--------------------------------------------
 http-nio-8080-exec-3 ------------------=--------------------------------------------
 http-nio-8080-exec-3           authType=null
 http-nio-8080-exec-3        contentType=null
 http-nio-8080-exec-3         remoteUser=null
 http-nio-8080-exec-3             status=404
 http-nio-8080-exec-3 END TIME          =26-Oct-2019 09:57:27
 http-nio-8080-exec-3 ===============================================================
 Processing ErrorPage[errorCode=0, location=/error]
  Disabling the response for further output
 Socket: [org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper@22e73315:org.apache.tomcat.util.net.NioChannel@756305f3:java.nio.channels.SocketChannel[connected local=/127.0.0.1:8080 remote=/127.0.0.1:54862]], Read from buffer: [0]