当前位置: 首页 > 工具软件 > Bio4j > 使用案例 >

通过NDC为Log4j日志加上线程标识id的方法

饶曦之
2023-12-01

当我们在多线程的环境下需要跟踪某一线程的产生的日志,如果没有一个用于标注日志所对应的线程的标识,那么查日志会是一个很痛苦的过程。

在日志中为不同线程加上唯一的标识是一个不错的解决办法,Log4j已经其实已经提供了多种实现方式,这里要说的是通过DNC的方式为日志加上线程标识,其他的方法请参考这里:https://blog.csdn.net/liulin_good/article/details/5995884

假设:当我们收到一个请求之后,业务层或持久层报了错,我们如何跟踪判断这个请求从前到后的所有日志?

简单的说,spring容器会为每一个请求分配一个线程,既然是要从收到请求开始跟踪,那么我们就在controller层做切面,收到请求后创建id并push到DNC中,并在请求处理结束时后,清除掉我们push到DNC中的id,下面上代码:

controller:

@Controller
public class UserController {
	@Autowired
	UserService userService;

	@RequestMapping("/getUsername")
	@ResponseBody
	public User getUserById(Integer id) throws Exception {
		User user = userService.getUserById(id);
		return user;
	}
}

service:(故意写一个会报错的代码,用于一会测试)

@Service
public class UserServiceImpl implements UserService {
	@Autowired
	UserMapper userMapper;

	@Override
	public User getUserById(Integer id) throws Exception {
		try {
			int i = 1 / 0;
			System.out.println(i);
		} catch (Exception e) {
			LogUtils.info("UserServiceImpl执行出错,入参为:id=" + id);
			throw new Exception("出错啦!!!", e);
		}
		User user = userMapper.getUserById(id);
		return user;
	}

}

日志实现类:

package com.test.log;

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

public class LogAspect {
	Logger logger = Logger.getLogger(LogAspect.class);
	String logStr = null;

	/**
	 * 前置通知:在某连接点之前执行的通知,但这个通知不能阻止连接点前的执行
	 * 
	 * @param jp
	 *            连接点:程序执行过程中的某一行为
	 */
	public void doBefore(JoinPoint jp) {
		logStr = jp.getTarget().getClass().getName() + "类的" + jp.getSignature().getName() + "方法开始执行******Start******\n";
		logger.info(logStr);
	}

	/**
	 * 环绕通知:包围一个连接点的通知,可以在方法的调用前后完成自定义的行为,也可以选择不执行。
	 * 类似web中Servlet规范中Filter的doFilter方法。
	 * 
	 * @param pjp
	 *            当前进程中的连接点
	 * @return
	 */
	public Object doAround(ProceedingJoinPoint pjp) {
		Object result = null;
		try {
			result = pjp.proceed();
		} catch (Throwable e) {
			e.printStackTrace();
			StringBuilder logInfo = new StringBuilder(
					"方法:" + pjp.getTarget().getClass() + "." + pjp.getSignature().getName() + "()\n");
			logInfo.append("提示信息为:" + e.getMessage() + "\n");
			logInfo.append("具体错误信息如下:\n");
			StackTraceElement[] stackTrace = e.getStackTrace();
			for (StackTraceElement stackTraceElement : stackTrace) {
				logInfo.append(stackTraceElement + "\n");
			}
			logger.info(logInfo);
		}
		return result;
	}

	/**
	 * 后置通知
	 * 
	 * @param jp
	 */
	public void doAfter(JoinPoint jp) {
		logStr = jp.getTarget().getClass().getName() + "类的" + jp.getSignature().getName() + "方法执行结束******End******\n";
		logger.info(logStr);
	}
}

生成日志id实现类:

package com.test.log;

import java.util.UUID;

import org.apache.log4j.NDC;

public class LogId {

	/**
	 * 通过uuid为线程标注唯一标识
	 */
	public void pushLogId() {
		NDC.push(UUID.randomUUID().toString());
	}

	/**
	 * 记得清除一下
	 */
	public void removeLogId() {
		NDC.remove();
	}
}

在spring的配置文件applicationContext.xml中配置业务层和持久层的日志切面:

	<!-- 日志 -->
	<bean id="logAspect" class="com.test.log.LogAspect"></bean>
	<aop:config>
		<!-- controller日志 -->
		<aop:pointcut id="serviceLogPointcut"
			expression="execution(* com.test.*.*.*(..))" />
		<aop:aspect id="aspect" ref="logAspect">
			<aop:before pointcut-ref="serviceLogPointcut"
				method="doBefore" />
			<aop:after pointcut-ref="serviceLogPointcut"
				method="doAfter" />
			<aop:around pointcut-ref="serviceLogPointcut"
				method="doAround" />
		</aop:aspect>
	</aop:config>

在springMVC的配置文件springmvc.xml中配置controller的日志切面,并生成线程标识id:

	<bean id="logId" class="com.test.log.LogId"></bean>
	<aop:config>
		<aop:pointcut id="controllerLogPointcut"
			expression="execution(* com.test.controller.*.*(..))" />
		<!-- 生成日志id -->
		<aop:aspect id="logIdAspect" ref="logId" order="1">
			<aop:before pointcut-ref="controllerLogPointcut"
				method="pushLogId" />
			<aop:after pointcut-ref="controllerLogPointcut"
				method="removeLogId" />
		</aop:aspect>
		<!-- controller日志 -->
		<aop:aspect id="aspect" ref="logAspect" order="2">
			<aop:before pointcut-ref="controllerLogPointcut"
				method="doBefore" />
			<aop:after pointcut-ref="controllerLogPointcut"
				method="doAfter" />
			<aop:around pointcut-ref="controllerLogPointcut"
				method="doAround" />
		</aop:aspect>
	</aop:config>

在Log4j.properties中加上%x打印标识信息:

log4j.rootLogger=INFO, Console, R
#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=target/Test_Log4j.log
log4j.appender.R.MaxFileSize=100KB log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d{yyyy/MM/dd HH:mm:ss,SSS} %x %p %t %c - %m%n

环境搭建完成,我们访问两次,查看日志输出结果:

2018/06/03 23:22:18,050 58612924-106d-46ec-86d2-fe906f86ed58 INFO http-bio-8080-exec-1 com.test.log.LogAspect - com.test.controller.UserController类的getUserById方法开始执行******Start******

2018/06/03 23:22:18,072 58612924-106d-46ec-86d2-fe906f86ed58 INFO http-bio-8080-exec-1 com.test.log.LogAspect - com.test.service.impl.UserServiceImpl类的getUserById方法开始执行******Start******

2018/06/03 23:22:18,084 58612924-106d-46ec-86d2-fe906f86ed58 INFO http-bio-8080-exec-1 com.test.log.LogUtils - UserServiceImpl执行出错,入参为:id=1
2018/06/03 23:22:18,127 58612924-106d-46ec-86d2-fe906f86ed58 INFO http-bio-8080-exec-1 com.test.log.LogAspect - 方法:class com.test.service.impl.UserServiceImpl.getUserById()
提示信息为:出错啦!!!
具体错误信息如下:
com.test.service.impl.UserServiceImpl.getUserById(UserServiceImpl.java:23)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302)
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:85)
com.test.log.LogAspect.doAround(LogAspect.java:33)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:621)
org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:610)
org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:68)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
org.springframework.aop.aspectj.AspectJAfterAdvice.invoke(AspectJAfterAdvice.java:43)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:52)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208)
com.sun.proxy.$Proxy35.getUserById(Unknown Source)
com.test.controller.UserController.getUserById(UserController.java:19)
com.test.controller.UserController$$FastClassBySpringCGLIB$$8141ca2a.invoke(<generated>)
org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:720)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:85)
com.test.log.LogAspect.doAround(LogAspect.java:33)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:621)
org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:610)
org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:68)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
org.springframework.aop.aspectj.AspectJAfterAdvice.invoke(AspectJAfterAdvice.java:43)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:52)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
org.springframework.aop.aspectj.AspectJAfterAdvice.invoke(AspectJAfterAdvice.java:43)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:52)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:655)
com.test.controller.UserController$$EnhancerBySpringCGLIB$$4107c115.getUserById(<generated>)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:222)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:814)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:737)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:969)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:871)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:845)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:121)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
java.lang.Thread.run(Thread.java:748)

2018/06/03 23:22:18,130 58612924-106d-46ec-86d2-fe906f86ed58 INFO http-bio-8080-exec-1 com.test.log.LogAspect - com.test.service.impl.UserServiceImpl类的getUserById方法执行结束******End******

2018/06/03 23:22:18,130 58612924-106d-46ec-86d2-fe906f86ed58 INFO http-bio-8080-exec-1 com.test.log.LogAspect - com.test.controller.UserController类的getUserById方法执行结束******End******

2018/06/03 23:22:31,233 6c5ab650-8ce9-4ad8-b887-74d6b36f4c81 INFO http-bio-8080-exec-2 com.test.log.LogAspect - com.test.controller.UserController类的getUserById方法开始执行******Start******

2018/06/03 23:22:31,234 6c5ab650-8ce9-4ad8-b887-74d6b36f4c81 INFO http-bio-8080-exec-2 com.test.log.LogAspect - com.test.service.impl.UserServiceImpl类的getUserById方法开始执行******Start******

2018/06/03 23:22:31,234 6c5ab650-8ce9-4ad8-b887-74d6b36f4c81 INFO http-bio-8080-exec-2 com.test.log.LogUtils - UserServiceImpl执行出错,入参为:id=1
2018/06/03 23:22:31,275 6c5ab650-8ce9-4ad8-b887-74d6b36f4c81 INFO http-bio-8080-exec-2 com.test.log.LogAspect - 方法:class com.test.service.impl.UserServiceImpl.getUserById()
提示信息为:出错啦!!!
具体错误信息如下:
com.test.service.impl.UserServiceImpl.getUserById(UserServiceImpl.java:23)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302)
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:85)
com.test.log.LogAspect.doAround(LogAspect.java:33)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:621)
org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:610)
org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:68)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
org.springframework.aop.aspectj.AspectJAfterAdvice.invoke(AspectJAfterAdvice.java:43)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:52)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208)
com.sun.proxy.$Proxy35.getUserById(Unknown Source)
com.test.controller.UserController.getUserById(UserController.java:19)
com.test.controller.UserController$$FastClassBySpringCGLIB$$8141ca2a.invoke(<generated>)
org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:720)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:85)
com.test.log.LogAspect.doAround(LogAspect.java:33)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:621)
org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:610)
org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:68)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
org.springframework.aop.aspectj.AspectJAfterAdvice.invoke(AspectJAfterAdvice.java:43)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:52)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
org.springframework.aop.aspectj.AspectJAfterAdvice.invoke(AspectJAfterAdvice.java:43)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:52)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:655)
com.test.controller.UserController$$EnhancerBySpringCGLIB$$4107c115.getUserById(<generated>)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:222)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:814)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:737)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:969)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:871)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:845)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:121)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
java.lang.Thread.run(Thread.java:748)

2018/06/03 23:22:31,277 6c5ab650-8ce9-4ad8-b887-74d6b36f4c81 INFO http-bio-8080-exec-2 com.test.log.LogAspect - com.test.service.impl.UserServiceImpl类的getUserById方法执行结束******End******

2018/06/03 23:22:31,277 6c5ab650-8ce9-4ad8-b887-74d6b36f4c81 INFO http-bio-8080-exec-2 com.test.log.LogAspect - com.test.controller.UserController类的getUserById方法执行结束******End******

从日志上我们可以清晰的看到,第一次访问时的线程标识id为58612924-106d-46ec-86d2-fe906f86ed58,第二次访问为的线程标识id为6c5ab650-8ce9-4ad8-b887-74d6b36f4c81,测试成功。

通过这个id我们可以轻松的筛选出所有这次请求相关的日志信息。补充一下,如果想在程序中获取id(比如把返回给前台方便排错),可以在LogUtils里增加一个getId()的方法,返回NDC.peek()。

如果觉得uuid太长了还可以换成其他的id,只要能保证唯一性就行。


 类似资料: