当前位置: 首页 > news >正文

使用AOP实现打印日志

首先创建annotation.SystemLog类:

package com.gjh.annotation;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;@Target(ElementType.METHOD) //加在什么东西上
@Retention(RetentionPolicy.RUNTIME) //什么时候
public @interface SystemLog {String BusinessName(); //会以注解方式用在接口上
}

然后创建aspect.LogAspect:

package com.gjh.aspect;import com.alibaba.fastjson.JSON;
import com.gjh.annotation.SystemLog;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;import javax.servlet.http.HttpServletRequest;@Component
@Aspect
@Slf4j
public class LogAspect {@Pointcut("@annotation(com.gjh.annotation.SystemLog)")public void pt(){}@Around("pt()")public Object printLog(ProceedingJoinPoint joinPoint) throws Throwable {Object ret;try {handleBefore(joinPoint);ret = joinPoint.proceed();handleAfter(ret);} finally {// 结束后换行log.info("=======End=======" + System.lineSeparator());}return ret;}//切入点之前运行private void handleBefore(ProceedingJoinPoint joinPoint) {ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();HttpServletRequest request = requestAttributes.getRequest();//获取被增强的方法上的注解对象SystemLog systemLog = getSystemLog(joinPoint);log.info("=======Start=======");// 打印请求 URLlog.info("URL            : {}",request.getRequestURL());// 打印描述信息log.info("BusinessName   : {}", systemLog.BusinessName());// 打印 Http methodlog.info("HTTP Method    : {}",request.getMethod());// 打印调用 controller 的全路径以及执行方法log.info("Class Method   : {}.{}",joinPoint.getSignature().getDeclaringTypeName(),((MethodSignature)joinPoint.getSignature()).getName());// 打印请求的 IPlog.info("IP             : {}",request.getRemoteHost());// 打印请求入参log.info("Request Args   : {}", JSON.toJSONString(joinPoint.getArgs()));}private SystemLog getSystemLog(ProceedingJoinPoint joinPoint) {MethodSignature methodSignature =(MethodSignature) joinPoint.getSignature();SystemLog systemLog = methodSignature.getMethod().getAnnotation(SystemLog.class);return systemLog;}//切入点之后运行private void handleAfter(Object ret) {// 打印出参log.info("Response       : {}", JSON.toJSONString(ret));}
}

最后,在需要的接口上使用注解:

 @SystemLog(BusinessName = "更新用户信息")

 

例如我用在了:

@PutMapping("/userInfo")@Operation(tags = "修改个人信息")@SystemLog(BusinessName = "更新用户信息") //AOPpublic ResponseResult updateUserInfo(@RequestBody UserInfoDTO userInfoDTO){return ResponseResult.okResult(userservice.UpdateUserInfo(userInfoDTO));}

 

重启测试:

当我们进行修改个人信息之后:

e88c72a020b741608bd2bf75a843b3b0.png

 

http://www.lryc.cn/news/329727.html

相关文章:

  • 2024年新算法-冠豪猪优化算法(CPO),CPO-RF-Adaboost,CPO优化随机森林RF-Adaboost回归预测-附代码
  • 浅谈高阶智能驾驶-NOA领航辅助的技术与发展
  • 大模型 智能体 智能玩具 智能音箱 构建教程 wukong-robot
  • Clickhouse-表引擎探索之MergeTree
  • 网络电视盒子哪个好?小编分享电视盒子品牌排行榜
  • 开源模型应用落地-baichuan2模型小试-入门篇(三)
  • 景联文科技高质量大模型训练数据汇总!
  • 【python】正则表达式
  • 学习vue3第十二节(组件的使用与类型)
  • flume配置文件后不能跟注释!!
  • 【docker】Dockerfile自定义镜像
  • webpack项目打包console git分支、打包时间等信息 exec
  • Linux centos7离线搭建FTP
  • 关于GPT-SoVITS语音合成的效果展示(西游之西天送葬团)
  • 如何安装OceanBase的OBD
  • Unity 读写Excel打包后无法运行可能的解决方案
  • 算法沉淀 —— 深度搜索(dfs)
  • #设计模式#3.1用做松鼠桂鱼来理解抽象工厂(对象创建型模式)
  • adb基本命令
  • 小工具实战-Python实现小工具输出字符串大小写转换、字符串统计、编解码、MD5加密
  • MySQL进阶-----索引的语法与SQL性能分析
  • Ansible剧本playbooks详解
  • vue3封装Element导航菜单
  • 字符串的函数
  • Linux安装redis(基于CentOS系统,Ubuntu也可参考)
  • ChatGPT引领量化交易革命:AI在金融创新的浪潮中崭露头角
  • 无忧微服务:如何实现大流量下新版本的发布自由
  • Halcon3D表面平面度检测-平面差值法
  • golang 在多线程中避免 CPU 指令重排
  • 自动化更新包文件--shell脚本