springboot+aop切点记录请求和响应信息
本篇主要分享的是springboot中结合aop方式来记录请求参数和响应的数据信息;这里主要讲解两种切入点方式,一种方法切入,一种注解切入;首先创建个springboot测试工程并通过maven添加如下依赖:
<!-- AOP -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency> <!--阿里 FastJson依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.</version>
</dependency>
先来说方法的切点方式,需要创建个名为LogAspect的组件类,然后用@Aspect注解修饰组件类,再通过设置方法切入点方式做公共日志记录,如下创建切入点:
//切点入口 Controller包下面所有类的所有方法
private final String pointcut = "execution(* com.platform.Controller..*(..))"; //切点
@Pointcut(value = pointcut)
public void log() {
}
这里的execution( com.platform.Controller..(..))主要的意思是:切入点入口是Controller包下面所有类的所有方法;再来通过@Around环绕注解方法里面做请求参数和响应信息的记录,如下代码:
@Around(value = "log()")
public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
Object result = null;
StringBuilder sbLog = new StringBuilder("\n");
try {
sbLog.append(String.format("类名:%s\r\n", proceedingJoinPoint.getTarget().getClass().getName())); MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();
sbLog.append(String.format("方法:%s\r\n", methodSignature.getMethod().getName())); Object[] args = proceedingJoinPoint.getArgs();
for (Object o : args) {
sbLog.append(String.format("参数:%s\r\n", JSON.toJSON(o)));
} long startTime = System.currentTimeMillis();
result = proceedingJoinPoint.proceed();
long endTime = System.currentTimeMillis();
sbLog.append(String.format("返回:%s\r\n", JSON.toJSON(result)));
sbLog.append(String.format("耗时:%ss", endTime - startTime));
} catch (Exception ex) {
sbLog.append(String.format("异常:%s", ex.getMessage()));
} finally {
logger.info(sbLog.toString());
}
return result;
}
此刻主要代码就完成了,再来我们配置下日志的记录方式;首先在 resources目录增加名为logback-spring.xml的文件,其配置信息如:
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Author:shenniu003
~ Copyright (c) .
--> <configuration debug="false" scan="true" scanPeriod="1 seconds"> <springProperty scope="context" name="appname" source="logging.logback.appname"/>
<springProperty scope="context" name="logLevel" source="logging.logback.level"/>
<springProperty scope="context" name="logPath" source="logging.logback.path"/> <property name="logPathAll" value="${logPath}/${appname}.log"/> <contextName>logback</contextName> <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<!-- <filter class="ch.qos.logback.classic.filter.ThresholdFilter" >
<level>WARN</level>
</filter>-->
<encoder>
<pattern>%d{HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{} - %msg%n</pattern>
</encoder>
</appender> <appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${logPathAll}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${logPathAll}.%d{yyyy-MM-dd}.zip</fileNamePattern>
</rollingPolicy>
<encoder>
<pattern>%date %level [%thread] %logger{} [%file : %line] %msg%n
</pattern>
</encoder>
</appender> <root level="${logLevel}">
<appender-ref ref="console"/>
<appender-ref ref="file"/>
</root> </configuration>
然后application.yml的配置信息如:
logging:
config: classpath:logback-spring.xml
logback:
level: info #info ,debug
path: /home/app/data/applogs/weblog
appname: web
此刻日志和公共的aop记录类都完成了,我们需要创建个测试用例,其代码如:
@PostMapping("/addUser")
public ResponseEntity<MoStudent> addUser(@RequestBody MoStudent moStudent) throws Exception {
moStudent.setNumber(UUID.randomUUID().toString());
// throw new Exception("错误了");
return new ResponseEntity<>(moStudent, HttpStatus.OK);
}
最后,通过postman模拟post请求,能够得到如下的日志结果:

上面的方式切入点是所有方法,所有方法都记录日志可能有是不是需求想要的,此时可以通过注解的方式来标记想记录日志的方法;先来创建个日志注解:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface LogAnnotation {
/**
* 描述
*
* @return
*/
String des() default "";
}
同样再来创建注解的切入点:
//匹配方法上包含此注解的方法
private final String annotationPointCut = "@annotation(com.platform.Aop.LogAnnotation)"; //注解切点
@Pointcut(value = annotationPointCut)
public void logAnnotation() {
}
再通过@Around注解绑定具体的操作方法:
@Around(value = "logAnnotation()")
public Object aroundAnnotation(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
Object result = null;
StringBuilder sbLog = new StringBuilder("\n");
try {
sbLog.append(String.format("类名:%s\r\n", proceedingJoinPoint.getTarget().getClass().getName())); MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();
Method method = methodSignature.getMethod();
LogAnnotation logAnnotation = method.getAnnotation(LogAnnotation.class);
if (logAnnotation != null && !logAnnotation.des().isEmpty()) {
sbLog.append(String.format("说明:%s\r\n", logAnnotation.des()));
}
sbLog.append(String.format("方法:%s\r\n", method.getName())); Object[] args = proceedingJoinPoint.getArgs();
for (Object o : args) {
sbLog.append(String.format("参数:%s\r\n", JSON.toJSON(o)));
} long startTime = System.currentTimeMillis();
result = proceedingJoinPoint.proceed();
long endTime = System.currentTimeMillis();
sbLog.append(String.format("返回:%s\r\n", JSON.toJSON(result)));
sbLog.append(String.format("耗时:%ss", endTime - startTime));
} catch (Exception ex) {
sbLog.append(String.format("异常:%s", ex.getMessage()));
} finally {
logger.info(sbLog.toString());
}
return result;
}
这个方法里需要注意的是注解方式相比第一种其实就多了如下几行代码:
Method method = methodSignature.getMethod();
LogAnnotation logAnnotation = method.getAnnotation(LogAnnotation.class);
if (logAnnotation != null && !logAnnotation.des().isEmpty()) {
sbLog.append(String.format("说明:%s\r\n", logAnnotation.des()));
}
下面是注解测试用例:
@LogAnnotation(des = "注解记录日志")
@PostMapping("/addUser01")
public ResponseEntity<MoStudent> addUser01(@RequestBody MoStudent moStudent) throws Exception {
moStudent.setNumber(UUID.randomUUID().toString());
return new ResponseEntity<>(moStudent, HttpStatus.OK);
}

如下是LogAspect.java的所有代码:
/*
* Author:shenniu003
* Copyright (c) 2018.
*/ package com.platform.Aop; import com.alibaba.fastjson.JSON;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component; import java.lang.reflect.Method; /**
* Created by Administrator on 2018/11/5.
* springboot+aop切点记录请求和响应信息
*/
@Component
@Aspect
public class LogAspect { //切点入口 Controller包下面所有类的所有方法
private final String pointcut = "execution(* com.platform.Controller..*(..))"; //匹配方法上包含此注解的方法
private final String annotationPointCut = "@annotation(com.platform.Aop.LogAnnotation)"; private Logger logger = LoggerFactory.getLogger(LogAspect.class); //切点
@Pointcut(value = pointcut)
public void log() {
} @Around(value = "log()")
public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
Object result = null;
StringBuilder sbLog = new StringBuilder("\n");
try {
sbLog.append(String.format("类名:%s\r\n", proceedingJoinPoint.getTarget().getClass().getName())); MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();
sbLog.append(String.format("方法:%s\r\n", methodSignature.getMethod().getName())); Object[] args = proceedingJoinPoint.getArgs();
for (Object o : args) {
sbLog.append(String.format("参数:%s\r\n", JSON.toJSON(o)));
} long startTime = System.currentTimeMillis();
result = proceedingJoinPoint.proceed();
long endTime = System.currentTimeMillis();
sbLog.append(String.format("返回:%s\r\n", JSON.toJSON(result)));
sbLog.append(String.format("耗时:%ss", endTime - startTime));
} catch (Exception ex) {
sbLog.append(String.format("异常:%s", ex.getMessage()));
} finally {
logger.info(sbLog.toString());
}
return result;
} //注解切点
@Pointcut(value = annotationPointCut)
public void logAnnotation() {
} @Around(value = "logAnnotation()")
public Object aroundAnnotation(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
Object result = null;
StringBuilder sbLog = new StringBuilder("\n");
try {
sbLog.append(String.format("类名:%s\r\n", proceedingJoinPoint.getTarget().getClass().getName())); MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();
Method method = methodSignature.getMethod();
LogAnnotation logAnnotation = method.getAnnotation(LogAnnotation.class);
if (logAnnotation != null && !logAnnotation.des().isEmpty()) {
sbLog.append(String.format("说明:%s\r\n", logAnnotation.des()));
}
sbLog.append(String.format("方法:%s\r\n", method.getName())); Object[] args = proceedingJoinPoint.getArgs();
for (Object o : args) {
sbLog.append(String.format("参数:%s\r\n", JSON.toJSON(o)));
} long startTime = System.currentTimeMillis();
result = proceedingJoinPoint.proceed();
long endTime = System.currentTimeMillis();
sbLog.append(String.format("返回:%s\r\n", JSON.toJSON(result)));
sbLog.append(String.format("耗时:%ss", endTime - startTime));
} catch (Exception ex) {
sbLog.append(String.format("异常:%s", ex.getMessage()));
} finally {
logger.info(sbLog.toString());
}
return result;
}
}
springboot+aop切点记录请求和响应信息的更多相关文章
- Spring Boot使用AOP在控制台打印请求、响应信息
AOP称为面向切面编程,在程序开发中主要用来解决一些系统层面上的问题,比如日志,事务,权限等. AOP简介 AOP全称Aspect Oriented Programming,面向切面,AOP主要实现的 ...
- openresty(完整版)Lua拦截请求与响应信息日志收集及基于cjson和redis动态路径以及Prometheus监控(转)
直接上文件 nginx.conf #运行用户和组,缺省为nobody,若改为别的用户和组,则需要先创建用户和组 #user wls81 wls; #开启进程数,一般与CPU核数等同 worker_pr ...
- nginx log记录请求的头信息
记录访问的log,为了在出现特殊情况时,方便检查出现问题的地方.log_format accesslog ‘$remote_addr – $remote_user [$time_local] “$re ...
- 第14.14节 爬虫实战准备:csdn博文点赞过程http请求和响应信息分析
如果要对csdn博文点赞,首先要登录CSDN,然后打开一篇需要点赞的文章,如<第14.1节 通过Python爬取网页的学习步骤>按<第14.3节 使用google浏览器获取网站访问的 ...
- 使用RestTemplate,显示请求信息,响应信息
使用RestTemplate,显示请求信息,响应信息 这里不讲怎么用RestTemplate具体细节用法,就是一个学习中的过程记录 一个简单的例子 public class App { public ...
- Fiddler如何自动修改请求和响应包
Charles的Map功能可以将某个请求进行重定向,用重定向的内容响应请求的内容.这个功能非常方便.在抓包过程当中,有时候为了调试方便,需要将线上的服务定位到内网.比如我们线上的服务器域名为 api. ...
- springboot aop 自定义注解方式实现完善日志记录(完整源码)
版权声明:本文为博主原创文章,欢迎转载,转载请注明作者.原文超链接 一:功能简介 本文主要记录如何使用aop切面的方式来实现日志记录功能. 主要记录的信息有: 操作人,方法名,参数,运行时间,操作类型 ...
- springboot aop 自定义注解方式实现一套完善的日志记录(完整源码)
https://www.cnblogs.com/wenjunwei/p/9639909.html https://blog.csdn.net/tyrant_800/article/details/78 ...
- Spring Boot中使用AOP记录请求日志
这周看别人写的springboot后端代码中有使用AOP记录请求日志,以前没接触过,因此学习下. 一.AOP简介 AOP为Aspect Oriented Programming的缩写,意为:面向切面编 ...
随机推荐
- java基础之修饰符和内部类
1.java修饰符 /* 修饰符: 权限修饰符:private,默认的,protected,public 状态修饰符:static,final 抽象修饰符:abstract 类: 权限修饰符:默认修饰 ...
- kafka实战
1. kafka介绍 1.1. 主要功能 根据官网的介绍,ApacheKafka®是一个分布式流媒体平台,它主要有3种功能: 1:It lets you publish and ...
- Redis Rpop 命令
Redis Rpop 命令用于移除并返回列表的最后一个元素. 语法 redis Rpop 命令基本语法如下: redis 127.0.0.1:6379> RPOP KEY_NAME 可用版本 & ...
- machine learning 之 Anomaly detection
自Andrew Ng的machine learning课程. 目录: Problem Motivation Gaussian Distribution Algorithm Developing and ...
- protocol_v2.go
{ return protocol.NewFatalClientErr(nil, "E_INVALID", fmt.Sprintf(&quo ...
- fasthttp中的协程池实现
fasthttp中的协程池实现 协程池可以控制并行度,复用协程.fasthttp 比 net/http 效率高很多倍的重要原因,就是利用了协程池.实现并不复杂,我们可以参考他的设计,写出高性能的应用. ...
- 如何使用Docker部署一个Go Web应用程序
熟悉Docker如何提升你在构建.测试并部署Go Web应用程序的方式,并且理解如何使用Semaphore来持续部署. 简介 大多数情况下Go应用程序被编译成单个二进制文件,web应用程序则会包括模版 ...
- ||与&&的返回值
当你准备携带你的配剑杀向江湖的时候,当你准备进入js这门语言的时候,你会遇到很多||与&&的问题.那么对于他们的返回值你知道多少呢? 在此之前我们来聊一个大家都知道的知识:js中值转换 ...
- QTimer在QThread环境中失效的问题
QTimer在非QThread的环境下能正常工作.但在QThread环境下,需要做一些改动才能正常工作. 创建Qt的线程有两种方式: 1. 子例化QThread 可以在虚函数run中启动定时器,大致的 ...
- 我眼中的 Nginx(六):深入 Nginx/Openresty 服务里的 DNS 解析
张超:又拍云系统开发高级工程师,负责又拍云 CDN 平台相关组件的更新及维护.Github ID: tokers,活跃于 OpenResty 社区和 Nginx 邮件列表等开源社区,专注于服务端技术的 ...