Spring Boot & Slf4j 2 Http 请求元数据日志记录拦截器?



我想配置我的日志记录设置,以便将传入的请求标记为 id(即 UUID(,该 id 用于跟踪整个应用程序中的请求和进程(即控制器类、服务类等(以及请求中的其他相关信息(即主机名、IP 地址......

参考 slf4j 2 文档,我看到提到了 ThreadContext。对于向我的 spring-boot 应用程序发出的每个请求,我想做类似的事情:

ThreadContext.put("id", UUID.randomUUID().toString());
ThreadContext.put("ipAddress", this.request.getRemoteAddr());
...

并在调试器的模式布局中调用这些键,如下所示:%X{key}。理想情况下,这将产生类似于以下内容的内容:

[INFO] request #UUID1 #IP1: start.
[INFO] request #UUID1 #IP1: do something controller class 
[INFO] request #UUID2 #IP2: start.
[INFO] request #UUID1 #IP1: do something service class
[INFO] request #UUID2 #IP2: do something controller class 
[INFO] request #UUID2 #IP2: do something service class 
[INFO] request #UUID1 #IP1: end.
[INFO] request #UUID2 #IP2: end.

虽然我不确定如何开始实际实施它。任何见解将不胜感激!

您应该使用 slf4j 查看 MDC(映射诊断上下文( https://ivanursul.com/slf4j-mdc

创建一个 Servlet 过滤器,它在 MDC 中设置所需的参数,它们将在整个请求中可用,并自动记录。

Spring Cloud Sleuth 将为您完成所有这些工作......只需将其包含在您的类路径中,并将您关心的属性添加到您的日志记录输出中。它还会自动检测控制器和客户端以传递标头属性。

使用实现 Filter 类的类,我能够使用以下代码以及调用 Log4j 2 PatternLayout 中的键来获得所需的行为:

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.ThreadContext;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import javax.servlet.Filter;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.FilterChain;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.TimeZone;
import java.util.UUID;
@Component
public class RequestResponseLoggingFilter implements Filter {
private FilterConfig filterConfig;
private static String TZ_NAME = "timezoneOffset";
private static final Logger LOGGER = LogManager.getLogger(RequestBody.class);

@Override
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
}
/**
* Sample filter that populates the MDC on every request.
*/
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)servletRequest;
HttpServletResponse response = (HttpServletResponse)servletResponse;
String requestId = request.getHeader("requestId");
if (requestId == null) {
requestId = UUID.randomUUID().toString();
}
ThreadContext.put("requestId", requestId);
ThreadContext.put("ipAddress", request.getRemoteAddr());
HttpSession session = request.getSession(false);
TimeZone timeZone = null;
ThreadContext.put("hostname", servletRequest.getServerName());
ThreadContext.put("locale", servletRequest.getLocale().getDisplayName());
if (timeZone == null) {
timeZone = TimeZone.getDefault();
}
ThreadContext.put("timezone", timeZone.getDisplayName());
LOGGER.info(
"Logging Request {} : {}", request.getMethod(),
request.getRequestURI());
filterChain.doFilter(servletRequest, servletResponse);
LOGGER.info(
"Logging Response {} : {}",
response.getStatus(), response.getContentType());
ThreadContext.clearMap();
}
@Override
public void destroy() {
}
}

相关内容

  • 没有找到相关文章