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.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); } }


作者:周六不算加班
鏈接:https://www.jianshu.com/p/a0f3adced194
來源:簡書
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM