Logback日志添加唯一追踪ID
发布日期:2021-06-30 12:36:45 浏览次数:4 分类:技术文章

本文共 3974 字,大约阅读时间需要 13 分钟。

问题描述

日志是排查问题的重要依据,但是有时不容易定位报错信息,不明确哪条错误信息对应哪次请求。如果给日志加上唯一的追踪ID,排查就方便多了。

每个请求打印的日志都会有不同的ID,哪个请求出错,根据ID来定位日志。这个ID还会返回给前端,前端发现报错直接把ID给后台人员进行定位分析。

开发环境
  • SpringBoot - 2.1.10.RELEASE
  • JDK 1.8
创建过滤器
package com.nobody.filter;import java.io.IOException;import java.util.UUID;import javax.servlet.Filter;import javax.servlet.FilterChain;import javax.servlet.FilterConfig;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import org.slf4j.MDC;/** * 过滤器,每次请求设置日志唯一追踪ID *  * @author Μr.ηobοdy * * @date 2020-04-29 * */public class LogMDCFilter implements Filter {
@Override public void init(FilterConfig filterConfig) throws ServletException {
} @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
String requestIdKey = "requestId"; String requestId = UUID.randomUUID().toString().replace("-", ""); MDC.put(requestIdKey, requestId); try {
filterChain.doFilter(servletRequest, servletResponse); } finally {
MDC.remove(requestIdKey); } } @Override public void destroy() {
}}
注册过滤器
package com.nobody.ehr.config;import org.springframework.boot.web.servlet.FilterRegistrationBean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import com.nobody.ehr.filter.LogMDCFilter;/** * 注册日志过滤器 *  * @author Μr.ηobοdy * * @date 2020-04-29 * */@Configurationpublic class FilterConfig {
@Bean public FilterRegistrationBean
logFilterRegistration() {
FilterRegistrationBean
registration = new FilterRegistrationBean
(); // 注入过滤器 registration.setFilter(new LogMDCFilter()); // 拦截规则 registration.addUrlPatterns("/*"); // 过滤器名称 registration.setName("logMDCFilter"); // 过滤器顺序 registration.setOrder(0); return registration; }}
返回值封装
package com.nobody.ehr.exception;import org.slf4j.MDC;import lombok.Getter;import lombok.Setter;import lombok.ToString;/** * 异常数据封装 *  * @author Μr.ηobοdy * * @date 2020-03-12 * */@Getter@Setter@ToStringpublic class RestErrorData {
private String error; private int error_code; private String error_description; private String error_uri; private String requestId = MDC.get("requestId"); public RestErrorData() {
super(); } public RestErrorData(String error, int error_code, String error_description) {
super(); this.error = error; this.error_code = error_code; this.error_description = error_description; } public RestErrorData(RestAPIError restAPIError) {
super(); this.error = restAPIError.error; this.error_code = restAPIError.error_code; this.error_description = restAPIError.error_description; }}
logback.xml文件,其中%X{requestId}就是唯一追踪ID的值
${
layout}
./logs/server.log
./logs/server%d{
yyyy-MM-dd}_%i.log
${
maxHistory}
${
maxFileSize}
UTF-8
${
layout}
0
./logs/motan.log
./logs/motan.log.%d{
yyyy-MM-dd}.%i
${
maxHistory}
${
maxFileSize}
UTF-8
${
layout}
0
验证结果:

在这里插入图片描述

在这里插入图片描述

转载地址:https://javalib.blog.csdn.net/article/details/105844926 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Spring IOC 容器源码分析
下一篇:Log4j格式化符号%详解

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月21日 07时53分31秒