Before the article

Are you still using Logback for Spring Boot log processing?

In this article

The previous chapter focuses on the configuration of Log4j2. This chapter focuses on the application of unified log processing, including HTTP request log processing and Exception log processing.

HTTP Request Logs

img

1. Specify the log content

Example: User, IP address, Method, URI, request parameters, request body

2, global intercept mdcfilter.java

package com.anoyi.config.server; import lombok.extern.log4j.Log4j2; import org.slf4j.MDC; import org.springframework.http.HttpMethod; import org.springframework.stereotype.Component; import org.springframework.web.filter.OncePerRequestFilter; import javax.servlet.FilterChain; import javax.servlet.ReadListener; import javax.servlet.ServletException; import javax.servlet.ServletInputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; import javax.servlet.http.HttpServletResponse; import java.io.*; /** * */ @component @log4j2 public class MDCFilter extends OncePerRequestFilter {@override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException { try { MDC.put("user", request.getRemoteUser()); String query = request.getQueryString() ! = null ? "?" + request.getQueryString() : ""; if (request.getMethod().equals(HttpMethod.POST.name())) { MultiReadHttpServletRequest multiReadHttpServletRequest = new MultiReadHttpServletRequest(request); log.info("IP:{}, Method:{}, URI:{} Body:{}", request.getRemoteAddr(), request.getMethod(), request.getRequestURI() + query, multiReadHttpServletRequest.getRequestBody()); chain.doFilter(multiReadHttpServletRequest, response); } else { log.info("IP:{}, Method:{}, URI:{}", request.getRemoteAddr(), request.getMethod(), request.getRequestURI() + query); chain.doFilter(request, response); } } finally { MDC.clear(); }} / * * * it request body read * / class MultiReadHttpServletRequest extends HttpServletRequestWrapper {/ / cache RequestBody private String requestBody; MultiReadHttpServletRequest(HttpServletRequest request) { super(request); requestBody = ""; try { StringBuilder stringBuilder = new StringBuilder(); InputStream inputStream = request.getInputStream(); byte[] bs = new byte[1024]; int len; while ((len = inputStream.read(bs)) ! = -1) { stringBuilder.append(new String(bs, 0, len)); } requestBody = stringBuilder.toString(); } catch (IOException e) { e.printStackTrace(); } } @Override public ServletInputStream getInputStream() throws IOException { final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(requestBody.getBytes()); return new ServletInputStream() { public int read() throws IOException { return byteArrayInputStream.read(); } @Override public boolean isFinished() { return byteArrayInputStream.available() == 0; } @Override public boolean isReady() { return true; } @Override public void setReadListener(ReadListener readListener) { } }; } @Override public BufferedReader getReader() throws IOException { return new BufferedReader(new InputStreamReader(this.getInputStream())); } String getRequestBody() { return requestBody.replaceAll("\n", ""); }}}Copy the code

3. Configure log Pattern

Logging: the pattern: the console: "% d {MM - dd yyyy - HH: MM: ss. The SSS} % 5 p 15.15 t] [% % - 40.40 - c {1} {user}] [% X: % m % n % xwEx"Copy the code

txt

Mdc. put(“user”, request.getremoteuser ()); => %X{user}

© copyright belongs to the author, reprint or content cooperation please contact the author

● Spring Boot custom parent quickly build applications

● Spring Boot container deployment – Docker

● SpringBot teaches you how to configure HTTPS

Are you still using Logback?

● How does the micro-service registry carry tens of millions of visits of large systems?

Here’s how you should play it

● Spring Boot exception processing

● Spring Boot Configuration – Configuration information encryption

● Reject black box applications – Visual monitoring of Spring Boot applications

● There are three sources of concurrency bugs, so keep your eyes open

This article is published by OpenWrite!