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

Springboot切面打印日志

切面打印完整日志,以下代码用于扫描@RestController 注解修饰的接口,并打印相关日志

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;@Aspect
@Component
public class RequestLoggingAspect {private final Logger logger = LoggerFactory.getLogger(RequestLoggingAspect.class);@Pointcut("within(@org.springframework.web.bind.annotation.RestController *)")public void controller() {}@Before("controller()")public void logBefore(JoinPoint joinPoint) {ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();HttpServletRequest request = attributes.getRequest();logger.info("Request URL: {}", request.getRequestURL());logger.info("HTTP Method: {}", request.getMethod());logger.info("IP Address: {}", request.getRemoteAddr());logger.info("Class Method: {}.{}", joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName());logger.info("Request Parameters: {}", Arrays.toString(joinPoint.getArgs()));}@AfterReturning(pointcut = "controller()", returning = "result")public void logAfterReturning(JoinPoint joinPoint, Object result) {logger.info("Response: {}", result);}
}

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

相关文章:

  • ubuntu上回环设备/dev/loop0占用100%清理
  • List list=new ArrayList()抛出的ArrayIndexOutOfBoundsException异常
  • 桶排序算法
  • P8604 [蓝桥杯 2013 国 C] 危险系数
  • Excel·VBA表格横向、纵向相互转换
  • Leetcode-每日一题【剑指 Offer 06. 从尾到头打印链表】
  • LeetCode--HOT100题(22)
  • 产品体系架构202308版
  • Linux systemctl 简单介绍与使用
  • 恺英网络宣布:与华为鸿蒙系统展开合作,将开发多款手游
  • Vue CORS
  • Godot 4 源码分析 - 文件读入编码处理
  • Linux 中使用 verdaccio 搭建私有npm 服务器
  • C++入门之stl六大组件--stack和queue源码深度剖析及模拟实现
  • MyCat配置文件schema.xml讲解
  • Grafana集成prometheus(2.Grafana安装)
  • 代码随想录算法训练营第五十七天| 647. 回文子串 516.最长回文子序列
  • django 优化方式
  • IDEA中怎么使用git下载项目到本地,通过URL克隆项目(giteegithub)
  • 09. Docker Compose
  • 如何在shell脚本将node_modules里的文件复制一份到public文件里
  • 监控Redis的关键指标
  • Openlayers和leaflet如何选用?
  • 跟我学C++中级篇——三五法则
  • aardio:用 WebView 模仿 mdict 界面
  • linq中的操作符
  • 数据结构【哈夫曼树】
  • SpringMVC基于SpringBoot的最基础框架搭建——包含数据库连接
  • deepspeed zero3
  • 代驾小程序怎么做