springboot aop 自定义注解方式实现一套完善的日志记录

本文展示了如何利用SpringBoot的AOP功能,通过自定义注解来实现全面的日志记录。主要内容包括功能简介、项目结构、代码实现、Maven依赖以及运行结果。涉及的操作包括记录操作人、方法名、参数、运行时间、操作类型和返回值等关键信息。
摘要由CSDN通过智能技术生成

一:功能简介

本文主要记录如何使用aop切面的方式来实现日志记录功能。

主要记录的信息有: 操作人,方法名,参数,运行时间,操作类型(增删改查),详细描述,返回值。

二:项目结构图

 

 

如果想学习Java工程化、高性能及分布式、深入浅出。微服务、Spring,MyBatis,Netty源码分析的朋友可以加我的Java高级交流:854630135,群里有阿里大牛直播讲解技术,以及Java大型互联网技术的视频免费分享给大家。

三:代码实现

1.配置文件

 
这里只有两个配置: 1)server.port=11000,设置项目启动的端口号,防止被其他服务占用; 2)spring.aop.auto=true,开启spring的aop配置,简单明了,不需要多配置其他的配置或注解。 application.yml文件 server:  port: 11000 spring:  aop:  auto: true #启动aop配置 

2.AOP切点类

这个是最主要的类,可以使用自定义注解或针对包名实现AOP增强。

1)这里实现了对自定义注解的环绕增强切点,对使用了自定义注解的方法进行AOP切面处理;

2)对方法运行时间进行监控;

3)对方法名,参数名,参数值,对日志描述的优化处理;

在方法上增加@Aspect 注解声明切面,使用@Pointcut 注解定义切点,标记方法。

使用切点增强的时机注解:@Before,@Around,@AfterReturning,@AfterThrowing,@After

 
package com.wwj.springboot.aop; import com.alibaba.fastjson.JSON; import com.wwj.springboot.annotation.OperationLogDetail; import com.wwj.springboot.model.OperationLog; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.stereotype.Component; import java.util.Date; import java.util.HashMap; import java.util.Map; import java.util.UUID; /** * Created by IntelliJ IDEA * * @author weiwenjun * @date 2018/9/12 */ @Aspect @Component public class LogAspect { /** * 此处的切点是注解的方式,也可以用包名的方式达到相同的效果 * '@Pointcut("execution(* com.wwj.springboot.service.impl.*.*(..))")' */ @Pointcut("@annotation(com.wwj.springboot.annotation.OperationLogDetail)") public void operationLog(){} /** * 环绕增强,相当于MethodInterceptor */ @Around("operationLog()") public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable { Object res = null; long time = System.currentTimeMillis(); try { res = joinPoint.proceed(); time = System.currentTimeMillis() - time; return res; } finally { try { //方法执行完成后增加日志 addOperationLog(joinPoint,res,time); }catch (Exception e){ System.out.println("LogAspect 操作失败:" + e.getMessage()); e.printStackTrace(); } } } private void addOperationLog(JoinPoint joinPoint, Object res, long time){ MethodSignature signature = (MethodSignature)joinPoint.getSignature(); OperationLog operationLog = new OperationLog(); operationLog.setRunTime(time); operationLog.setReturnValue(JSON.toJSONString(res)); operationLog.setId(UUID.randomUUID().toString()); operationLog.setArgs(JSON.toJSONString(joinPoint.getArgs())); operationLog.setCreateTime(new Date()); operationLog.setMethod(signature.getDeclaringTypeName() + "." + signature.getName()); operationLog.setUserId("#{currentUserId}"); operationLog.setUserName("#{currentUserName}"); OperationLogDetail annotation = signature.getMethod().getAnn
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值