spring boot aop Log Management (MongoDB)

aop controller layer requests are intercepted, the request in the normal @Before to intercept,
abnormal @AfterThrowing to intercept the request by
1, reference JAR package aop

    <dependency>
        <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> <version>2.0.3.RELEASE</version> </dependency> 

2, code implementation

@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.getClass().getName()); log.info("异常信息:" + e.getMessage()); 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(e.getMessage()); mongoTemplate.save(operationLog); } }


Author: Saturday overtime not
link: https: //www.jianshu.com/p/a0f3adced194
Source: Jane books
are copyrighted by the author. Commercial reprint please contact the author authorized, non-commercial reprint please indicate the source.

Guess you like

Origin www.cnblogs.com/liuxiaofu/p/12005746.html