Java SpringBoot 日志
某些业务需求需要追踪接口访问情况,也就是把请求和响应记录下来。基本的记录维度包含了请求入参(路径query参数,请求体)、请求路径(uri)、请求方法(method)、请求头(headers)以及响应状态、响应头、甚至包含了敏感的响应体等等。总结了几种方法,可以按需选择。

请求追踪的实现方式

网关层

很多网关设施都具有httptrace的功能,可以帮助集中记录请求流量的情况。Orange、Kong、Apache Apisix这些基于Nginx的网关都具有该能力,就连Nginx本身也提供了记录httptrace日志的能力。
优点是可以集中的管理httptrace日志,免开发;缺点是技术要求高,需要配套的分发、存储、查询的设施。

Spring Boot Actuator

在Spring Boot中,其实提供了简单的追踪功能。只需要集成:

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-actuator</artifactId>
  4. </dependency>

开启/actuator/httptrace:

  1. management:
  2. endpoints:
  3. web:
  4. exposure:
  5. include: 'httptrace'

就可以通过http://server:port/actuator/httptrace获取最近的Http请求信息了。
不过在最新的版本中可能需要显式的声明这些追踪信息的存储方式,也就是实现HttpTraceRepository接口并注入Spring IoC。
例如放在内存中并限制为最近的100条(不推荐生产使用):

  1. @Bean
  2. public HttpTraceRepository httpTraceRepository(){
  3. return new InMemoryHttpTraceRepository();
  4. }

追踪日志以json格式呈现:
2021-08-11-20-04-33-543883.png
Spring Boot Actuator记录的httptrace
记录的维度不多,当然如果够用的话可以试试。
优点在于集成起来简单,几乎免除开发;缺点在于记录的维度不多,而且需要搭建缓冲消费这些日志信息的设施。

CommonsRequestLoggingFilter

Spring Web模块还提供了一个过滤器CommonsRequestLoggingFilter,它可以对请求的细节进行日志输出。配置起来也比较简单:

  1. @Bean
  2. CommonsRequestLoggingFilter loggingFilter(){
  3. CommonsRequestLoggingFilter loggingFilter = new CommonsRequestLoggingFilter();
  4. // 记录 客户端 IP信息
  5. loggingFilter.setIncludeClientInfo(true);
  6. // 记录请求头
  7. loggingFilter.setIncludeHeaders(true);
  8. // 如果记录请求头的话,可以指定哪些记录,哪些不记录
  9. // loggingFilter.setHeaderPredicate();
  10. // 记录 请求体 特别是POST请求的body参数
  11. loggingFilter.setIncludePayload(true);
  12. // 请求体的大小限制 默认50
  13. loggingFilter.setMaxPayloadLength(10000);
  14. //记录请求路径中的query参数
  15. loggingFilter.setIncludeQueryString(true);
  16. return loggingFilter;
  17. }

而且必须开启对CommonsRequestLoggingFilterdebug日志:

  1. logging:
  2. level:
  3. org:
  4. springframework:
  5. web:
  6. filter:
  7. CommonsRequestLoggingFilter: debug

一次请求会输出两次日志,一次是在第一次经过过滤器前;一次是完成过滤器链后。
2021-08-11-20-04-33-620885.png
CommonsRequestLoggingFilter记录请求日志
这里多说一句其实可以改造成输出json格式的。
优点是灵活配置、而且对请求追踪的维度全面,缺点是只记录请求而不记录响应。

ResponseBodyAdvice

Spring Boot统一返回体其实也能记录,需要自行实现。这里借鉴了CommonsRequestLoggingFilter解析请求的方法。响应体也可以获取了,不过响应头和状态因为生命周期还不清楚,这里获取还不清楚是否合适,不过这是一个思路。

  1. @Slf4j
  2. @RestControllerAdvice(basePackages = {"cn.fcant.logging"})
  3. public class RestBodyAdvice implements ResponseBodyAdvice<Object> {
  4. private static final int DEFAULT_MAX_PAYLOAD_LENGTH = 10000;
  5. public static final String REQUEST_MESSAGE_PREFIX = "Request [";
  6. public static final String REQUEST_MESSAGE_SUFFIX = "]";
  7. private ObjectMapper objectMapper = new ObjectMapper();
  8. @Override
  9. public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
  10. return true;
  11. }
  12. @SneakyThrows
  13. @Override
  14. public Object beforeBodyWrite(Object body,
  15. MethodParameter returnType,
  16. MediaType selectedContentType,
  17. Class<? extends HttpMessageConverter<?>> selectedConverterType,
  18. ServerHttpRequest request,
  19. ServerHttpResponse response) {
  20. ServletServerHttpRequest servletServerHttpRequest = (ServletServerHttpRequest) request;
  21. log.debug(createRequestMessage(servletServerHttpRequest.getServletRequest(), REQUEST_MESSAGE_PREFIX, REQUEST_MESSAGE_SUFFIX));
  22. Rest<Object> objectRest;
  23. if (body == null) {
  24. objectRest = RestBody.okData(Collections.emptyMap());
  25. } else if (Rest.class.isAssignableFrom(body.getClass())) {
  26. objectRest = (Rest<Object>) body;
  27. }
  28. else if (checkPrimitive(body)) {
  29. return RestBody.okData(Collections.singletonMap("result", body));
  30. }else {
  31. objectRest = RestBody.okData(body);
  32. }
  33. log.debug("Response Body ["+ objectMapper.writeValueAsString(objectRest) +"]");
  34. return objectRest;
  35. }
  36. private boolean checkPrimitive(Object body) {
  37. Class<?> clazz = body.getClass();
  38. return clazz.isPrimitive()
  39. || clazz.isArray()
  40. || Collection.class.isAssignableFrom(clazz)
  41. || body instanceof Number
  42. || body instanceof Boolean
  43. || body instanceof Character
  44. || body instanceof String;
  45. }
  46. protected String createRequestMessage(HttpServletRequest request, String prefix, String suffix) {
  47. StringBuilder msg = new StringBuilder();
  48. msg.append(prefix);
  49. msg.append(request.getMethod()).append(" ");
  50. msg.append(request.getRequestURI());
  51. String queryString = request.getQueryString();
  52. if (queryString != null) {
  53. msg.append('?').append(queryString);
  54. }
  55. String client = request.getRemoteAddr();
  56. if (StringUtils.hasLength(client)) {
  57. msg.append(", client=").append(client);
  58. }
  59. HttpSession session = request.getSession(false);
  60. if (session != null) {
  61. msg.append(", session=").append(session.getId());
  62. }
  63. String user = request.getRemoteUser();
  64. if (user != null) {
  65. msg.append(", user=").append(user);
  66. }
  67. HttpHeaders headers = new ServletServerHttpRequest(request).getHeaders();
  68. msg.append(", headers=").append(headers);
  69. String payload = getMessagePayload(request);
  70. if (payload != null) {
  71. msg.append(", payload=").append(payload);
  72. }
  73. msg.append(suffix);
  74. return msg.toString();
  75. }
  76. protected String getMessagePayload(HttpServletRequest request) {
  77. ContentCachingRequestWrapper wrapper =
  78. WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class);
  79. if (wrapper != null) {
  80. byte[] buf = wrapper.getContentAsByteArray();
  81. if (buf.length > 0) {
  82. int length = Math.min(buf.length, DEFAULT_MAX_PAYLOAD_LENGTH);
  83. try {
  84. return new String(buf, 0, length, wrapper.getCharacterEncoding());
  85. } catch (UnsupportedEncodingException ex) {
  86. return "[unknown]";
  87. }
  88. }
  89. }
  90. return null;
  91. }
  92. }

别忘记配置ResponseBodyAdvice的logging级别为DEBUG

logstash-logback-encoder

这个是logstash的logback编码器,可以结构化输出httptrace为json。引入:

  1. <dependency>
  2. <groupId>net.logstash.logback</groupId>
  3. <artifactId>logstash-logback-encoder</artifactId>
  4. <version>6.6</version>
  5. </dependency>

配置logback的ConsoleAppenderLogstashEncoder:

  1. <configuration>
  2. <appender name="jsonConsoleAppender" class="ch.qos.logback.core.ConsoleAppender">
  3. <encoder class="net.logstash.logback.encoder.LogstashEncoder"/>
  4. </appender>
  5. <root level=" INFO">
  6. <appender-ref ref="jsonConsoleAppender"/>
  7. </root>
  8. </configuration>

然后同样实现一个解析的Filter:

  1. import org.slf4j.Logger;
  2. import org.slf4j.LoggerFactory;
  3. import org.slf4j.MDC;
  4. import org.springframework.core.annotation.Order;
  5. import org.springframework.stereotype.Component;
  6. import javax.servlet.*;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import java.io.IOException;
  10. import java.util.UUID;
  11. @Order(1)
  12. @Component
  13. public class MDCFilter implements Filter {
  14. private final Logger LOGGER = LoggerFactory.getLogger(MDCFilter.class);
  15. private final String X_REQUEST_ID = "X-Request-ID";
  16. @Override
  17. public void doFilter(ServletRequest request,
  18. ServletResponse response,
  19. FilterChain chain) throws IOException, ServletException {
  20. HttpServletRequest req = (HttpServletRequest) request;
  21. HttpServletResponse res = (HttpServletResponse) response;
  22. try {
  23. addXRequestId(req);
  24. LOGGER.info("path: {}, method: {}, query {}",
  25. req.getRequestURI(), req.getMethod(), req.getQueryString());
  26. res.setHeader(X_REQUEST_ID, MDC.get(X_REQUEST_ID));
  27. chain.doFilter(request, response);
  28. } finally {
  29. LOGGER.info("statusCode {}, path: {}, method: {}, query {}",
  30. res.getStatus(), req.getRequestURI(), req.getMethod(), req.getQueryString());
  31. MDC.clear();
  32. }
  33. }
  34. private void addXRequestId(HttpServletRequest request) {
  35. String xRequestId = request.getHeader(X_REQUEST_ID);
  36. if (xRequestId == null) {
  37. MDC.put(X_REQUEST_ID, UUID.randomUUID().toString());
  38. } else {
  39. MDC.put(X_REQUEST_ID, xRequestId);
  40. }
  41. }
  42. }

这里解析方式其实还可以更加精细一些。
然后所有的日志都可以结构化为json了:

  1. {
  2. "@timestamp":"2021-08-10T23:48:51.322+08:00",
  3. "@version":"1",
  4. "message":"statusCode 200, path: /log/get, method: GET, query foo=xxx&bar=ooo",
  5. "logger_name":"cn.fcant.logging.MDCFilter",
  6. "thread_name":"http-nio-8080-exec-1",
  7. "level":"INFO",
  8. "level_value":20000,
  9. "X-Request-ID":"7c0db56c-b1f2-4d85-ad9a-7ead67660f96"
  10. }