How to configure access log entries for a Spring Boot App
search cancel

How to configure access log entries for a Spring Boot App

book

Article ID: 294498

calendar_today

Updated On:

Products

VMware Spring Runtime

Issue/Introduction

By default, Gorouter generates access log entries for requests to your application and these show up in your application log stream.

For troubleshooting purposes, it can also be helpful to have access to log entries from a Spring Boot application. For example, this is useful when you are trying to troubleshoot Gorouter to see if it is adding additional latency to a request.

Environment


Resolution

There are three ways to enable access logging for a Spring Boot application.


1. Using application.yml or application.properties

For this option, simply add the following to your application.yml file:
server:
  tomcat:
    accesslog:
      enabled: true
      directory: "/dev"
      prefix: stdout
      buffered: false
      suffix:
      file-date-format:
      pattern: "[ACCESS] %{org.apache.catalina.AccessLog.RemoteAddr}r %l %t %D %F %B %S vcap_request_id:%{X-Vcap-Request-Id}i"
If you're using an application.properties file, add the following to the file: 
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:
server.tomcat.accesslog.pattern: "[ACCESS] %{org.apache.catalina.AccessLog.RemoteAddr}r %l %t %D %F %B %S vcap_request_id:%{X-Vcap-Request-Id}i"
Then build a new JAR or WAR file and `cf push` your new file to Cloud Foundry.


2. Using environment variables

If you do not want to modify the application or cannot modify the application, you can make the same configuration change using environment variables. To do this, edit your application.yml file and add an `env:` block with the following variables:
...
  env:
    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:
    SERVER_TOMCAT_ACCESSLOG_PATTERN: "[ACCESS] %{org.apache.catalina.AccessLog.RemoteAddr}r %l %t %D %F %B %S vcap_request_id:%{X-Vcap-Request-Id}i"
Alternatively, you could run `cf set-env` once for each variable and set them this way. You could also set them through Apps Manager.

After setting the environment variables or adding them to your manifest.yml, run `cf push` to redeploy the application. After the app restarts, you should see access log entries for every request to the application through `cf logs`.


3. Access log entries

Access log entries with the following command: 
2019-04-16T14:25:08.18-0400 [APP/PROC/WEB/0] OUT [ACCESS] 10.10.10.10 - [16/Apr/2019:18:25:08 +0000] 55 54 23320 - vcap_request_id:3008bcd7-d7f1-442f-42c1-0f30de4b1083
The format above contains the following information, which is the default information used by the Java buildpack when it configures Tomcat:
  • remote client IP (after adjustment by the remote ip valve)
  • remote user (usually just `-`)
  • timestamp of the request
  • Time taken to process the request in millis
  • Time taken to commit the response, in millis
  • Bytes sent, excluding HTTP headers
  • User session ID (if one exists)
  • VCAP Request ID (for tracing/correlation purposes)
Adjust this format by modifying the "pattern" configuration option. The list of values supported by Tomcat can be found here.