Skip to content

Commit

Permalink
添加logback2相关内容
Browse files Browse the repository at this point in the history
  • Loading branch information
mason committed Nov 29, 2019
1 parent 754edbc commit 0558e4f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@

2. `logback-advance-demo`

- 示例功能:正在编写......
- 文章:正在编写......
- 示例功能:对 logback 的进阶使用进行描述,主要包括日志异步输出,多环境日志配置以及使用MDC进行分布式系统请求追踪。
- 文章:[springboot+logback日志输出企业实践(下)]( https://mianshenglee.github.io/2019/11/29/logback2.html )

# 如何找到我的更多文章

- [我的Blog](https://mianshenglee.github.io)`https://mianshenglee.github.io/`

- 我的公众号:
- 我的公众号(搜索`Mason技术记录`

![mason](https://gitee.com/mianshenglee/datastorage/raw/master/md-photo/myphoto/wx/wx-public.jpg)

Expand Down
4 changes: 2 additions & 2 deletions springboot-logback-demo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@

2. `logback-advance-demo`

- 示例功能:正在编写......
- 文章:正在编写......
- 示例功能:对 logback 的进阶使用进行描述,主要包括日志异步输出,多环境日志配置以及使用MDC进行分布式系统请求追踪。
- 文章:[springboot+logback日志输出企业实践(下)]( https://mianshenglee.github.io/2019/11/29/logback2.html )
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,22 @@ public class RequestIdTraceInterceptor implements HandlerInterceptor {

public static final String REQUEST_ID_KEY = "request-id";

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
MDC.put(REQUEST_ID_KEY, getRequestId(request));
return true;
}

@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
//把requestId添加到响应头,以便其它应用使用
response.addHeader(REQUEST_ID_KEY, MDC.get(REQUEST_ID_KEY));
//请求完成,从MDC中移除requestId
MDC.remove(REQUEST_ID_KEY);
}

/**
* 根据请求参数或请求头判断是否有“x-request-id”,有则使用,无则创建
* 根据请求参数或请求头判断是否有“request-id”,有则使用,无则创建
*
* @param request
* @return 返回x-request-id的唯一标识
Expand All @@ -34,26 +48,12 @@ public static String getRequestId(HttpServletRequest request) {
String headerRequestId = request.getHeader(REQUEST_ID_KEY);

if (parameterRequestId == null && headerRequestId == null) {
log.debug("no x-request-id in request parameter or header");
log.debug("no request-id in request parameter or header");
requestId = IdUtil.simpleUUID();
} else {
requestId = parameterRequestId != null ? parameterRequestId : headerRequestId;
}

return requestId;
}

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
MDC.put(REQUEST_ID_KEY, getRequestId(request));
return true;
}

@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
//把requestId添加到响应头,以便其它应用使用
response.addHeader(REQUEST_ID_KEY, MDC.get(REQUEST_ID_KEY));
//请求完成,从MDC中移除requestId
MDC.remove(REQUEST_ID_KEY);
}
}

0 comments on commit 0558e4f

Please sign in to comment.