springboot-aop日志打印
package com.cinc.ecmp.client; import com.cinc.ecmp.enums.BackResultEnum;
import com.cinc.ecmp.exception.BasException;
import com.cinc.ecmp.utils.JsonUtil;
import com.cinc.ecmp.vo.BackResult;
import lombok.extern.slf4j.Slf4j;
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.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;
import java.lang.reflect.Method; /**
* @Author: hhr
* @Despriction: 调用其他服务的日志打印
* @CreatedTime: 2019/8/13 14:01
* @ModifyBy:
* @ModifyTime:
* @ModifyDespriction:
* @Version: V1.0.0
*/
@Slf4j
@Component
@Aspect
public class ClientLogRecordAspect { private final String pointCutStr = "execution(* com.cinc.ecmp.client..*.*(..))"; /**
* 定义切点
*/
@Pointcut(pointCutStr)
public void executeService(){ } @Before("executeService()")
public void doBefore(JoinPoint point) throws Throwable{ ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest(); //从切面织入点处通过反射机制获取织入点处的方法
MethodSignature signature = (MethodSignature) point.getSignature();
String declar = signature.getName();
//获取切入点所在的方法
Method method = signature.getMethod();
log.info("============ 请求{}/{}开始 start:============",request.getRequestURI(),declar); //获取请求的类名
String className = point.getTarget().getClass().getName();
//获取请求的方法名
String methodName = method.getName();
//请求的参数
Object[] args = point.getArgs();
//将参数所在的数组转换成json
String params = JsonUtil.objectToJsonStr(args); log.info("{} 参数params:{}", declar,params); } @AfterReturning(value = pointCutStr,returning = "resultInfo")
public void doAfterReturn(JoinPoint point,Object resultInfo) throws Throwable{ ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest(); //从切面织入点处通过反射机制获取织入点处的方法
MethodSignature signature = (MethodSignature) point.getSignature();
String declar = signature.getName();
//获取切入点所在的方法
Method method = signature.getMethod(); log.info("{}返回参数params:{}",declar, JsonUtil.objectToJsonStr(resultInfo)); BackResult backResult = (BackResult)resultInfo; if (null == backResult || !BackResultEnum.SUCCESS.getCode().equals(backResult.getCode())){
throw new BasException("ERROR","调用" + declar + "失败");
} log.info("============ 请求{}/{}结束 end ============" , request.getRequestURI(),declar);
} }
springboot-aop日志打印的更多相关文章
- SpringBoot统一日志打印
统一日志打印 @Slf4j @Aspect @Component public class ControllerLog { private static final ThreadLocal<Lo ...
- java aop 日志打印 正则设置
package tz.lion.Utils.aop; import com.alibaba.fastjson.JSON;import org.springframework.web.multipart ...
- 基于XML配置的AOP实现日志打印
Spring中可以使用注解或XML文件配置的方式实现AOP.1.导入jar包 com.springsource.net.sf.cglib -2.2.0.jar com.springsource.org ...
- 简单的aop实现日志打印(切入点表达式)
Spring中可以使用注解或XML文件配置的方式实现AOP. 1.导入jar包 com.springsource.net.sf.cglib -2.2.0.jar com.springsource.or ...
- SpringBoot AOP处理请求日志处理打印
SpringBoot AOP处理请求日志处理打印 @Slf4j @Aspect @Configuration public class RequestAopConfig { @Autowired pr ...
- springboot aop + logback + 统一异常处理 打印日志
1.src/resources路径下新建logback.xml 控制台彩色日志打印 info日志和异常日志分不同文件存储 每天自动生成日志 结合myibatis方便日志打印(debug模式) < ...
- Springboot + SLF4j + Log4j2 打印异常日志时,耗时要5-6秒
1.使用jps -l 查看springboot项目的进程ID 2.使用命令jstack -l 进程ID > log.txt 打印堆栈信息到文件,内容如下: "http-nio-8065 ...
- 自定义springboot - starter 实现日志打印,并支持动态可插拔
1. starter 命名规则: springboot项目有很多专一功能的starter组件,命名都是spring-boot-starter-xx,如spring-boot-starter-loggi ...
- spring-boot 使用Aop通知打印控制器请求报文和返回报文
一.简介 开发过程中我们往往需要写许多例如: @GetMapping("/id/get") public Result getById( String id) throws Exc ...
- Springboot的日志管理&Springboot整合Junit测试&Springboot中AOP的使用
==============Springboot的日志管理============= springboot无需引入日志的包,springboot默认已经依赖了slf4j.logback.log4j等日 ...
随机推荐
- 第一次作业:reading and prepare
这个作业属于哪个课程 课程的链接 这个作业要求在哪里 作业要求的链接 我在这个课程的目标是 理解软件开发流程,更好的开发自己的软件 这个作业在哪个具体方面帮助我实现目标 对"工程" ...
- JMeter与LoadRunner的对比
1. 界面.安装.协议支持.函数库.成本.开源 2. 都可以实现分布式负载,相对来说LoadRunner更强大一些 3. 都支持在windows和linux环境的负载生成器.控制台方面,Jmeter跨 ...
- ubuntu16.04如何查看内存和CPU的使用情况
ubuntu16.04如何查看内存和CPU的使用情况? 使用一下命令: gnome-system-monitor
- MySQL主备模式的数据一致性解决方案
根据阿里交易型业务的特点,以及在双十一这样业内罕有的需求推动下,我们在官方的MySQL基础上增加了非常多实用的功能.性能补丁.而在使用MySQL的过程中,数据一致性是绕不开的话题之一.本文主要从阿里 ...
- JavaScript--查看代码运行效率console.time()与console.timeEnd()用法
程序运行时间计算: <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- SWF在线绘本批量制作高质量PDF的新方法(重点在批量制作)
SWF在线绘本批量制作高质量PDF的新方法(重点在批量制作) 2012-12-21 未来决定... http://www.ebama.net/thread-107643-1-1.html ...
- hdu2044 dp
/* 每一种情况都可以由周围2个点得出 */ #include<stdio.h> int main() { __int64 dp[]; int i,t,l,r; dp[]=; dp[]=; ...
- Java练习 SDUT-2787_加密术
加密术 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 加密技术是一种常用的安全保密手段,利用加密技术可以把重要的数据变 ...
- MyBatis映射文件的基本功能
#{}与${}区别 "#{}"使用的是preparedStatement方式预处理,就是使用了占位符来填充数据防止SQL注入. ${}使用的是statement方式进行sql语句的 ...
- margin负边距的使用(超简单)
写在开头: 在css的世界中,一切都是框,所有的框都处于流动的状态 margin负边距可以使文档流发生偏移 在没有设置margin-bottom的时候,parent的高度会跟随child的内部元素 ...