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日志打印的更多相关文章

  1. SpringBoot统一日志打印

    统一日志打印 @Slf4j @Aspect @Component public class ControllerLog { private static final ThreadLocal<Lo ...

  2. java aop 日志打印 正则设置

    package tz.lion.Utils.aop; import com.alibaba.fastjson.JSON;import org.springframework.web.multipart ...

  3. 基于XML配置的AOP实现日志打印

    Spring中可以使用注解或XML文件配置的方式实现AOP.1.导入jar包 com.springsource.net.sf.cglib -2.2.0.jar com.springsource.org ...

  4. 简单的aop实现日志打印(切入点表达式)

    Spring中可以使用注解或XML文件配置的方式实现AOP. 1.导入jar包 com.springsource.net.sf.cglib -2.2.0.jar com.springsource.or ...

  5. SpringBoot AOP处理请求日志处理打印

    SpringBoot AOP处理请求日志处理打印 @Slf4j @Aspect @Configuration public class RequestAopConfig { @Autowired pr ...

  6. springboot aop + logback + 统一异常处理 打印日志

    1.src/resources路径下新建logback.xml 控制台彩色日志打印 info日志和异常日志分不同文件存储 每天自动生成日志 结合myibatis方便日志打印(debug模式) < ...

  7. Springboot + SLF4j + Log4j2 打印异常日志时,耗时要5-6秒

    1.使用jps -l 查看springboot项目的进程ID 2.使用命令jstack -l 进程ID > log.txt 打印堆栈信息到文件,内容如下: "http-nio-8065 ...

  8. 自定义springboot - starter 实现日志打印,并支持动态可插拔

    1. starter 命名规则: springboot项目有很多专一功能的starter组件,命名都是spring-boot-starter-xx,如spring-boot-starter-loggi ...

  9. spring-boot 使用Aop通知打印控制器请求报文和返回报文

    一.简介 开发过程中我们往往需要写许多例如: @GetMapping("/id/get") public Result getById( String id) throws Exc ...

  10. Springboot的日志管理&Springboot整合Junit测试&Springboot中AOP的使用

    ==============Springboot的日志管理============= springboot无需引入日志的包,springboot默认已经依赖了slf4j.logback.log4j等日 ...

随机推荐

  1. 【md5加密】不可逆之简单例子原理

    import hashlib def md5_get(data): ret = hashlib.md5("gfdwuqmo@md1.".encode("utf-8&quo ...

  2. day4new-转自金角大王

    Python之路,Day4 - Python基础4 (new版)   本节内容 迭代器&生成器 装饰器 Json & pickle 数据序列化 软件目录结构规范 作业:ATM项目开发 ...

  3. Effective C++: 08定制new和delete

    49:了解new-handler的行为 当operator new无法满足某一内存分配需求时,它会抛出异常(以前会返回一个null).在抛出异常之前,它会调用一个客户指定的错误处理函数,也就是所谓的n ...

  4. QT QSettings读写配置文件

    QT QSettings读写配置文件 需要用一个配置文件来保存程序的初始值,同时也需要做保存修改后的值. 那么借助于QSetting 就可以达到目的. 注意,生成的是 ini 文件! //1.创建和读 ...

  5. Oracle日期

    oracle 日期格式 to_date("要转换的字符串","转换的格式")   两个参数的格式必须匹配,否则会报错. 即按照第二个参数的格式解释第一个参数. ...

  6. linux之架构图和八台服务器

    (1) (2)

  7. 从 FastAdmin 看国内开源现状

    从 FastAdmin 看国内开源现状 在做 FastAdmin 之前看过太多的国内开源软件,很多没有建立起正反馈,最终而烂尾. 国内开源者有时候收到的是负反馈. 比如遇到问题埋怨开源作者,对开源作者 ...

  8. php代码在模板页的活用

  9. swap function & copy-and-swap idiom

    在C++中,众所周知在一个资源管理类(例如含有指向堆内存的指针)中需要重新定义拷贝构造函数.赋值运算符以及析构函数(Big Three),在新标准下还可能需要定义移动构造函数和移动赋值运算符(Big ...

  10. 说说iOS与内存管理(上)

    http://www.cocoachina.com/ios/20150625/12234.html 说起内存管理,看似老生常谈,而真正掌握内存管理的核心其实并不简单.ARC/MRR以及“谁分配谁就负责 ...