feat.logging: 添加请求ID和用户信息的日志关联

- 在 Constants 中添加 REQUEST_ID 常量
- 在 AuthFilter 中添加 userName 和 userId 的 MDC 记录
- 新增 RequestIdFilter过滤器,用于设置请求ID
- 更新 logback-spring.xml,添加日志关联信息的输出格式
This commit is contained in:
2025-04-06 15:33:41 +08:00
parent a3588b23bd
commit 08d187fcce
4 changed files with 51 additions and 1 deletions

View File

@@ -52,6 +52,7 @@ public class Constants {
public static final String CACHE_ORGANIZATION_PERMISSION_IDS = "organizationPermissionIds";
public static final String CACHE_USER_BY_ID = "userById";
public static final String CACHE_USER_RESOURCE_IDS = "userResourceIds";
public static final String REQUEST_ID = "requestId";
public static String SYSCONFIG_ENABLE_CAPTCHA = "enable_captcha";
public static String SYSCONFIG_ENABLE_REQUEST_SIGN = "enable_request_sign";
public static String SYSCONFIG_SESSION_TIMEOUT = "session_timeout";

View File

@@ -147,7 +147,8 @@ public class AuthFilter extends OncePerRequestFilter {
}
var user = Ctx.currentUser();
MDC.put("user", user.getUsername());
MDC.put("userName", user.getUsername());
MDC.put("userId", user.getId());
stopWatch.start("hasResource");

View File

@@ -0,0 +1,46 @@
package cn.lihongjie.coal.filter;
import cn.lihongjie.coal.common.Constants;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.MDC;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
import java.io.IOException;
import java.util.*;
@Component
@Order(-1)
@Slf4j
public class RequestIdFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(
HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
String requestId = request.getHeader("X-Request-Id");
if (StringUtils.isNotBlank(requestId)) {
MDC.put(Constants.REQUEST_ID, requestId);
} else {
MDC.put(Constants.REQUEST_ID, UUID.randomUUID().toString());
}
try {
filterChain.doFilter(request, response);
} finally {
MDC.remove(Constants.REQUEST_ID);
}
}
}

View File

@@ -3,6 +3,7 @@
<springProperty scope="context" name="app" source="spring.application.name"
defaultValue="spring"/>
<property name="LOG_CORRELATION_PATTERN" value="[ %X{requestId:-} ] [ %X{userId:-} %X{userName:-} ]"/>
<springProperty scope="context" name="profile" source="spring.profiles.active" defaultValue="default"/>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<property name="LOG_FILE" value="${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/${app}-${profile}.log"/>
@@ -10,6 +11,7 @@
<include resource="org/springframework/boot/logging/logback/file-appender.xml"/>
<logger name="org.quartz" level="WARN"/>
<root level="info">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>