惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
The Blog of Author Tim Ferriss
罗磊的独立博客
月光博客
月光博客
GbyAI
GbyAI
腾讯CDC
G
Google Developers Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
U
Unit 42
Engineering at Meta
Engineering at Meta
A
About on SuperTechFans
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
aimingoo的专栏
aimingoo的专栏
C
Check Point Blog
Y
Y Combinator Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Microsoft Azure Blog
Microsoft Azure Blog
I
InfoQ
雷峰网
雷峰网
B
Blog RSS Feed
美团技术团队
M
MIT News - Artificial intelligence
有赞技术团队
有赞技术团队
D
Docker

博客园 - GL_BKY

AI笔记 hippo4j线程池监控落地 Redisson实现分布式限流 Archery SQL 审核查询平台 mybatis-typeHandler解决表字段中json映射对象问题 ConcurrentSkipListMap实现随机数命中权重占比 面试常问题 自动代码质量分析示例搭建 Springboot监听多个端口 Git使用规范 一些问题 netty MQ Spring dubbo+zookeeper JVM 数据结构 redis JS前端加密JAVA后端解密详解
WebLogAspect
GL_BKY · 2023-02-13 · via 博客园 - GL_BKY
package ***;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.MDC;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@Aspect
@Slf4j
@Order
public class WebLogAspect {

  @Pointcut("execution(* com.yinker.datacenter.web..*(..))")
  public void webLog(){}

  @Around("webLog()")
  public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
    MDC.clear();
    //记录请求开始执行时间:
    long beginTime = System.currentTimeMillis();
    //获取请求信息
    String methodName = pjp.getSignature().getName();
    //获取请求参数:
    MethodSignature ms = (MethodSignature) pjp.getSignature();
    //获取请求参数类型
    String[] parameterNames = ms.getParameterNames();
    //获取请求参数值
    Object[] parameterValues = pjp.getArgs();
    Map<String,Object> params = Maps.newHashMap();
    for(int i=0;i<parameterNames.length;i++){
      params.put(parameterNames[i],parameterValues[i]);
    }
    Object result;
    try {
      result = pjp.proceed();
    } catch (Throwable throwable) {
      long cost = System.currentTimeMillis() - beginTime;
      log.info("[接口调用记录]:method:{},cost:{}ms,params:{},result:{}",methodName,cost,params,throwable.getMessage());
      throw throwable;
    }
    long cost = System.currentTimeMillis() - beginTime;
    log.info("[接口调用记录]:method:{},cost:{}ms,params:{},result:{}",methodName,cost,params,JSON.toJSONString(result, SerializerFeature.WRITE_MAP_NULL_FEATURES));
    MDC.clear();
    return result;
  }
}