spring boot aop日志管理(MongoDB)
aop拦截的是controller层请求,正常的请求用@Before来拦截,
异常的请求用@AfterThrowing来拦截
1、引用aop jar包
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> <version>2.0.3.RELEASE</version> </dependency>
2、代码实现
@Aspect @Component @Slf4j public class LogAop { @Autowired private MongoTemplate mongoTemplate; @Pointcut("execution(public * com.caody.muyi.controller.*.*(..))") public void logAop(){}; @Before("logAop()") public void around(JoinPoint joinPoint){ log.info("user:cdy"); log.info("time:"+new Date()); log.info("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature ().getName()); log.info("ARGS : " + Arrays.toString(joinPoint.getArgs())); OperationLog operationLog = new OperationLog(); operationLog.setLogname("cdy"); operationLog.setLogtype("业务日志"); operationLog.setCreatetime(new Date()); operationLog.setUserid(1); operationLog.setClassname(joinPoint.getSignature().getDeclaringTypeName()); operationLog.setMethod(joinPoint.getSignature().getName()); operationLog.setSucceed("成功"); operationLog.setMessage(""); mongoTemplate.save(operationLog); } // @AfterReturning(returning = "object", pointcut = "logAop()") // public void after(Object object){ // System.out.println(object); // log.info("RESPONSE : " + object); // } @AfterThrowing(pointcut = "logAop()", throwing="e") public void afterThrowing(JoinPoint joinPoint, Throwable e){ log.info("user:cdy"); log.info("time:"+new Date()); log.info("path : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature ().getName()); log.info("param : " + Arrays.toString(joinPoint.getArgs())); log.info("异常代码:" + e.
相关推荐
justlike 2020-09-02
牧场SZShepherd 2020-07-19
也许不会看见 2020-07-08
也许不会看见 2020-06-28
zmysna 2020-06-25
HappyHeng 2020-06-25
itjavashuai 2020-05-27
mituan 2020-05-26
zhongliwen 2020-05-11
smalllove 2020-05-09
neweastsun 2020-05-05
也许不会看见 2020-05-04
MicroBoy 2020-05-04
CallMeV 2020-05-02
方志朋 2020-05-01
横云断岭 2020-04-26
weigenzongderoot 2020-04-20