springboot_tomcat turns on the access log, monitors and prints each request address and response time

springboot_tomcat turns on the access log, monitors and prints each request address and response time

      1. Foreword
    1. Environment
    1. Configure the corresponding object information
    1. AccessLog configuration default value and description
    1. Configuration Example
    1. Description of pattern parameter composition
    1. Commonly used pattern configurations
    • 7.1 pattern default value
  • 7.2 Default configuration description
  • 7.3 Print the contents of cookies and headers in requests and responses
    1. Summary

1. Foreword

Springboot 1.x has a built-in tomcat server, and the access log can record some key information of each request and response, which is very helpful for us to troubleshoot and analyze system performance.
However, springboot does not enable access log by default. The following describes how to enable access log and some log format configurations.

2. Environment

  • jdk 8
  • srpingboot 1.56
  • windows 10

3. Configure the corresponding object information

Each configuration item in the yml file of springboot will have a corresponding java object corresponding to it. The @ConfigurationProperties annotation is generally used to automatically load the configuration on the object. If there is no added configuration item displayed in the yml, this class can also be used. Provides a default threshold. Tomcat's access log configuration also has corresponding configuration classes. as follows:

package org.springframework.boot.autoconfigure.web;
...
@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true)
public class ServerProperties
        implements EmbeddedServletContainerCustomizer, EnvironmentAware, Ordered {
    public static class Tomcat {
        public static class Accesslog {
            ...
        }
    }
} 
  • jarBag:spring-boot-autoconfigure-1.5.6.RELEASE.jar
  • Configuration class:org.springframework.boot.autoconfigure.web.ServerProperties.Tomcat.Accesslog
  • This class is an internal class. The location is: org.springframework.boot.autoconfigure.web.ServerProperties -> Tocat -> Accesslog

By analyzing the ServerProperties object, we can know the configuration path of access log in yml, namely: server.tomcat.accesslog.

4. AccessLog configuration default value and description

Properties Default Value Description
enabled false Enable access logs.
pattern “common” The format pattern of the access log.
directory “logs” Directory where log files are created. Can be relative to tomcat base directory or absolute directory.
prefix “access_log” Log file name prefix.
suffix “.log” Log file name suffix.
rotate true Default is true. This parameter determines whether the log file needs to be switched. If it is set to false, the log file will not be switched, that is, all files will be placed in the same log file, and the fileDateFormat parameter will also be ignored. Use this parameter with caution.
renameOnRotate false Whether to postpone including the date stamp in the file name. If it is true, the log file of the current day will not have a date suffix (such as: access_log.txt). When the date is switched every day, the file of the current day will be renamed to The date suffix of the previous day, and a new file for the current day is generated.
fileDateFormat “.yyyy-MM-dd” The date format in the log file name, the default is .yyyy-MM-dd.
requestAttributesEnabled false Set the request attributes for the IP address, hostname, protocol, and port for the request.
buffered true Buffers the output so that it is only flushed periodically.

5. Configuration Example

server:
  tomcat:
    accesslog:
      enabled: true
      pattern: '%{yyyy-MM-dd HH:mm:ss}t %a %A %m %s %D %b %I %U%q %{User-Agent}i'
      directory: D:\logs\projectName\
      prefix: accesslog
      suffix: .log
      fileDateFormat: _yyyyMMdd
      rotate: true
      renameOnRotate: false
      buffered: true
      requestAttributesEnabled: true 

6. Description of pattern parameter composition

The pattern attribute consists of a series of string parameters, each parameter is prefixed with “%”. Currently, the following parameters are supported:

  • %a – remote IP address
  • %A – local IP address
  • %b – The number of bytes sent (Bytes sent), excluding bytes of HTTP headers, if it is 0, '-' will be displayed
  • %B – Number of bytes sent (Bytes sent), excluding bytes of HTTP headers
  • %h – remote host name (show IP if resolveHosts is false)
  • %H – request protocol
  • %l – remote username, always '-' (Remote logical username from identd)
  • %m – request method (GET, POST, etc.)
  • %p – the local port to accept requests
  • %q – query string, if present, with a leading '?'
  • %r – the first line of the request (including the request method and the requested URI)
  • %s – HTTP status code of response (200, 404, etc.)
  • %S – the user's session ID
  • %t – Date and time, Common Log Format format, use %{yyyy-MM-dd HH:mm:ss}t to control formatted output.
  • %u – the authenticated remote user, displays '-' if it does not exist
  • %U – request URL path
  • %v – local service name
  • %D – Time to process the request, in milliseconds
  • %T – Time to process the request, in seconds
  • %I – The thread name of the current request (can compare later with stacktraces)

7. Commonly used pattern configurations

7.1 pattern default value

The pattern parameter has two default modes: common and combined mode. The usage method is:

server:
  tomcat:
    accesslog:
      pattern: combined #orcommon 

7.2 Default configuration description

  • common
  • Default configuration: %h %l %u %t %r %s %b
  • combined
  • Default configuration: %h %l %u %t %r %s %b %{Referer}i %{User-Agent}i
  • The pattern of the mode adds the parameter form of Referer and User-Agent headers. For example, %{User-Agent}i is the requested User-Agent value (client, browser).
  • Custom configuration
  • conventional
  • %{yyyy-MM-dd HH:mm:ss}t %a %A %m %s %D %b %I %U%q
  • Request time Remote IP Local IP Method status code Time consuming (milliseconds) Number of bytes sent Thread name Request address + query parameters
  • If the %A local IP option is not used for centralized log collection, there is little need to change the value.
  • regular+ua
  • %{yyyy-MM-dd HH:mm:ss}t %a %A %m %s %D %b %I %U%q %{User-Agent}i
  • Request time Remote IP Local IP Method status code Time consuming (milliseconds) Number of bytes sent Thread name Request address + Query parameters Request UA
  • If the %A local IP option is not used for centralized log collection, there is little need to change the value.
  • Do not write local IP
  • %{yyyy-MM-dd HH:mm:ss}t %a %m %s %D %b %I %U%q
  • Request time, remote IP, method status code, time taken (milliseconds), number of bytes sent, thread name, request address + query parameters
  • write useragent
  • %{yyyy-MM-dd HH:mm:ss}t %a %m %s %D %b %I %U%q %{User-Agent}i
  • Request time Remote IP Local IP Method status code Time consuming (milliseconds) Number of bytes sent Thread name Request address + Query parameters Request UA

7.3 Print the contents of cookies and headers in requests and responses

Access Log also supports cookies, request headers, response headers, Session or other object information in ServletRequest. The following xxx is a valid parameter name.
For example, when outputting User-Agent, it should be written as %{User-Agent}i.

  • %{xxx}i The value of the parameter specified in the request header.
  • %{xxx}o The value of the parameter specified in the response header.
  • %{xxx}c The value of the parameter specified in the cookie.
  • %{xxx}r ServletRequest object attribute value.
  • %{xxx}s HttpSession object property value.
  • %{xxx}p writes the local (server) port (xxxlocal) or the remote (client) port (xxx=remote).
  • %{xxx}t controls the formatted output of date and time.

8. Summary

By recording access log, you can check some key information of each request. Analyze information such as which addresses are commonly used for requests, which services are time-consuming, and which time periods have a large number of requests.
This information can be analyzed through log analysis tools or real-time statistics using the awk command.