When we are doing Java projects, we usually need to use the request log to check online problems. It will be more difficult when there are many logs and we need to find all the logs of the entire request. Therefore, it is necessary to tell the key logs of the same request to be connected with the same unique identifier when recording. This will make it easier to search. Let’s use java aop to implement the logging of the request id. (This supports child thread inherits the main thread request id)
1: First we need a log request link id facet class
Note: If multithreading is not considered (steps 2 and 3 can be ignored)
package .iMagine_pro.aop; import .iMagine_common.; import .iMagine_pro.; import .slf4j.Slf4j; import ; import ; import ; import ; import org.; import ; /** * @author Name One * @ClassName TraceIdAspect * @description: Log request link id facet processing * @datetime December 16, 2024 14:28 * @version: 1.0 */ @Slf4j @Aspect @Component public class TraceIdAspect { /** Link Tracking ID */ public final static String TRACE_ID = "TRACE_ID"; /** User */ public final static String USER = "USER"; /** * Link id point tangent definition */ @Pointcut("execution(* .iMagine_pro.controller.*.*(..))") public void TraceIdCut() { } /** * Link id is added */ @Before("TraceIdCut()") public void cutProcessBefore() { (TRACE_ID, ()); //The following code is to record user information, which is convenient and intuitive to identify log operator information. If not required, delete the following part String nickname = (); if (null == nickname){ nickname = "Tourist Visit"; } (USER, nickname); } /** * Link id clear */ @After("TraceIdCut()") public void cutProcessAfter() { (); } }
package .iMagine_common.utils; import ; /** * @author Name One * @ClassName UUIDUtil * @description: UUID tool class * @datetime April 23, 2024 11:49 * @version: 1.0 */ public class UUIDUtil { /** * Get UUID * * @return */ public static String getUUID() { //Produce uuid and remove the short horizontal line of uuid return ().toString().replace("-", ""); } }
2: Create a tool class that handles multithreaded link tracing
package .iMagine_common.utils; import .iMagine_common.; import org.; import ; /** * @author Name One * @ClassName ThreadMdcUtil * @description: Multithreaded link tracing tool class * @datetime December 16, 2024 14:57 * @version: 1.0 */ public class ThreadMdcUtil { // Get unique identifier public static String generateTraceId() { return (); } public static void setTraceIdIfAbsent() { if ((SysConstant.TRACE_ID) == null) { (SysConstant.TRACE_ID, generateTraceId()); } } /** * When the parent thread submits a task to the thread pool, copy the data in its own MDC to the child thread. * * @param runnable thread to execute * @param context mdc of parent thread * @return Tasks after link id are passed */ public static Runnable wrap(final Runnable runnable, final Map<String, String> context) { return () -> { if (context == null) { (); } else { (context); } setTraceIdIfAbsent(); try { (); } finally { (); } }; } }
Three: When sending tasks to the thread pool, do request link id delivery
/** * Add [ai server-related operations (psot)] tasks to thread pool * * @param task Added tasks */ public static void addSendPostThreadToThreadPool(SendPostThread task) { ("addSendPostThreadToThreadPool pool:{}, task:{}", pool, task); try { ((task, ())); } catch (Exception e) { ("addSendPostThreadToThreadPool error pool:{}, task:{}, e:", pool, task, e); } }
4: Add link tracking id to the log output format configuration
<!-- Log output format (in%X{TRACE_ID}yesmdcLink request insideidofkey,user-%X{USER}yes用户信息ofkey)--> <property name="ENCODER_PATTERN" value="%d{yyyy-MM-dd HH:mm:} [%thread] %X{TRACE_ID} user-%X{USER} %-5level %logger{32}-[%line] - %msg%n"/>
At this point, the spring project implements logging with requested link id and completes
This is the article about how Spring project implements logging with request link id. For more related Spring request id logging content, please search for my previous article or continue browsing the related articles below. I hope everyone will support me in the future!