SpringBoot AOP处理请求日志处理打印
SpringBoot AOP处理请求日志处理打印
@Slf4j
@Aspect
@Configuration
public class RequestAopConfig {
@Autowired
private HttpServletRequest request;
private static final ThreadLocal<Long> START_TIME_MILLIS = new ThreadLocal<>();
@Pointcut("execution(* com.xxx.xxx.xxx..*(..)) " +
"&&(@annotation(org.springframework.web.bind.annotation.PostMapping)" +
"||@annotation(org.springframework.web.bind.annotation.GetMapping)" +
"||@annotation(org.springframework.web.bind.annotation.PutMapping)" +
"||@annotation(org.springframework.web.bind.annotation.DeleteMapping))")
public void controllerMethodPointcut() {
}
/**
* 前置通知:在某连接点之前执行的通知,但这个通知不能阻止连接点之前的执行流程(除非它抛出一个异常)。
*
* @param joinPoint 参数
*/
@Before("controllerMethodPointcut()")
public void before(JoinPoint joinPoint) {
START_TIME_MILLIS.set(System.currentTimeMillis());
}
/**
* 后置通知:在某连接点正常完成后执行的通知,通常在一个匹配的方法返回的时候执行。
*
* @param joinPoint 参数
*/
@AfterReturning(value = "controllerMethodPointcut()", returning = "result")
public void afterReturning(JoinPoint joinPoint, Object result) {
String logTemplate = "--------------- 执行成功 ---------------\n请求开始---Send Request URL: {}, Method: {}, Params: {} \n请求方法---ClassName: {}, [Method]: {}, execution time: {}ms \n请求结束---Send Response Result: {}";
log.info(logTemplate, request.getRequestURL(), request.getMethod(), JSON.toJSONString(joinPoint.getArgs()), joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName(), (System.currentTimeMillis() - START_TIME_MILLIS.get()), JSON.toJSONString(result));
START_TIME_MILLIS.remove();
}
/**
* 异常通知:在方法抛出异常退出时执行的通知。
*
* @param joinPoint 参数
*/
@AfterThrowing(value = "controllerMethodPointcut()", throwing = "ex")
public void afterThrowing(JoinPoint joinPoint, Throwable ex) {
String logTemplate = "--------------- 执行失败 ---------------\n异常请求开始---Send Request URL: {}, Method: {}, Params: {} \n异常请求方法---ClassName: {}, [Method]: {}, execution time: {}ms \n异常请求结束---Exception Message: {}";
log.error(logTemplate, request.getRequestURL(), request.getMethod(), JSON.toJSONString(joinPoint.getArgs()), joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName(), (System.currentTimeMillis() - START_TIME_MILLIS.get()), ex.getMessage());
START_TIME_MILLIS.remove();
}
/**
* 最终通知。当某连接点退出的时候执行的通知(不论是正常返回还是异常退出)。
*
* @param joinPoint
*/
@After("controllerMethodPointcut()")
public void after(JoinPoint joinPoint) {
}
}
SpringBoot AOP处理请求日志处理打印的更多相关文章
- Spring Boot中使用AOP记录请求日志
这周看别人写的springboot后端代码中有使用AOP记录请求日志,以前没接触过,因此学习下. 一.AOP简介 AOP为Aspect Oriented Programming的缩写,意为:面向切面编 ...
- 使用SpringBoot AOP 记录操作日志、异常日志
平时我们在做项目时经常需要对一些重要功能操作记录日志,方便以后跟踪是谁在操作此功能:我们在操作某些功能时也有可能会发生异常,但是每次发生异常要定位原因我们都要到服务器去查询日志才能找到,而且也不能对发 ...
- AOP打印请求日志,打印返回值
@Aspect // 申明是个spring管理的bean @Component @Slf4j public class LogAspectServiceApi { private JSONObject ...
- SpringBoot使用logback输出日志并打印sql信息 --经典---
最近在学习springboot以及一些springcloud插件的使用,其中发现默认的配置并不能打印一些有用的日志,所以需要自定义一些日志输出方式以便于查看日志排查问题,目前只整理了两种使用方式,如下 ...
- Spring boot AOP 记录请求日志
如何将所有的通过url的请求参数以及返回结果都输出到日志中? 如果在controller的类中每个方法名都写一个log输出肯定是不明智的选择. 使用spring的AOP功能即可完成. 1. 在pom. ...
- springboot aop+@interface实现日志记录
一.基本概念 1.自定义注解 自定义注解我们必须了解四个元注解,什么是元注解?元注解指作用于注解之上的元数据或者元信息,简单通俗的讲,元注解就是注解的注解 . Documented与Inherited ...
- springboot AOP全局拦截日志记录
@Aspect@Component@Slf4jpublic class WebLogAspect { @Pointcut("execution(public * com.we.control ...
- Springboot AOP写操作日志 GET POST
pom.xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
- Spring Boot 2.0 教程 | AOP 切面统一打印请求日志
欢迎关注微信公众号: 小哈学Java 文章首发于个人网站 https://www.exception.site/springboot/spring-boot-aop-web-request 本节中,您 ...
随机推荐
- js 实现排序算法 -- 快速排序(Quick Sort)
原文: 十大经典排序算法(动图演示) 快速排序 快速排序的基本思想:通过一趟排序将待排记录分隔成独立的两部分,其中一部分记录的关键字均比另一部分的关键字小,则可分别对这两部分记录继续进行排序,以达到整 ...
- 安装 Kali Linux 2018.1 及之后的事
本文为原创文章,转载请标明出处 目录 制作U盘启动盘 安装 Kali Linux 之后的事 更新源 配置 Zsh 配置 Vim 修改 Firefox 语言为中文 安装 Gnome 扩展 美化 安装 G ...
- (一)mybatis简易搭建
mybatis(基础及其搭建) 声明:该文章及该分类中的内容均基于正在开发的项目和一本参考书(深入浅出MyBatis技术原理与实战 by 杨开振) 一.mybatis核心组件(简要介绍) Sql ...
- Linux设置redis密码登录
第一种:永久方式 redis设置密码访问 你的redis在真是环境中不可以谁想访问就可以访问,所以必须要设置密码 设置密码的流程如下: vim /etc/redis.conf 找到 #require ...
- HTTP的响应码?
响应头对浏览器来说很重要,它说明了响应的真正含义.例如200表示响应成功了,302表示重定向,这说明浏览器需要再发一个新的请求. l 200:请求成功,浏览器会把响应体内容(通常是html)显示在浏览 ...
- linux常用命令使用指南
<软件自动化测试开发>出版啦 1 系统相关 useradd/userdel 添加用户/删除用户 su 切换用户命令 ls 用于查看所有文件夹的命令 列出目录内容 ...
- 代工黑马,纬创如何强吞iPhone?
现在,智能手机市场非常得意兴阑珊,以苹果为首的最强大脑似乎再也想不出什么好的创意,iPhone7也只不过是旧机种的翻新款式,看上去跟一块板砖.一块镜子差不多:软体方面则出现了大批的"过度 ...
- 一起了解 .Net Foundation 项目 No.8
.Net 基金会中包含有很多优秀的项目,今天就和笔者一起了解一下其中的一些优秀作品吧. 中文介绍 中文介绍内容翻译自英文介绍,主要采用意译.如与原文存在出入,请以原文为准. IdentityModel ...
- 启动tomcat报错 Unable to process Jar entry [module-info.class] from Jar...[xxx.xx.jar!\] for annotations
Java Web 项目tomcat启动报错module-info.class 从git 上面拉下的项目,运行报错. jdk.maven配置正常 tomcat启动遇见的问题: Unable to pro ...
- Oracle密码验证函数与Create Profile
今天看到了一个oracle密码函数的东西,就在网上找文档自己做测试,刚开始看不懂,最后做完记录一下 密码函数的作用就是要将用户密码进行限制,比如申请一个网站的账号的时候,密码会要求你不少于8位,必须要 ...