zoukankan      html  css  js  c++  java
  • spring MVC模式拦截所有入口方法的入参出参打印

    import com.alibaba.fastjson.JSONObject;
    import com.alibaba.fastjson.serializer.SerializerFeature;
    import org.apache.commons.lang3.StringUtils;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.reflect.MethodSignature;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Component;
    
    import java.lang.reflect.Method;
    @Component
    @Aspect
    public class MvcMethodLogAdvice {
    
        private static Logger log = LoggerFactory.getLogger(MvcMethodLogAdvice.class);
    
        @Around("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
        public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
    
            Object args[] = joinPoint.getArgs();
            MethodSignature signature = (MethodSignature) joinPoint.getSignature();
            Method method = signature.getMethod();
            // join arguments.
            log.info("{}.{} : 请求参数:{} ", method.getDeclaringClass().getName(), method.getName(), StringUtils.join(args, " ; "));
            long start = System.currentTimeMillis();
            Object result = joinPoint.proceed();
            long timeConsuming = System.currentTimeMillis() - start;
            log.info("调用结束--> 返回值:{} 耗时:{}ms", JSONObject.toJSONString(result, SerializerFeature.WriteMapNullValue), timeConsuming);
            return result;
        }
    }

    在你的项目中加入这个类就好,这样设置之后,就会打印这样的日志

    对日后定位问题极有助益

  • 相关阅读:
    Object-C,NSSet,不可变集合
    NYIST 860 又见01背包
    NYIST 1070 诡异的电梯【Ⅰ】
    HDU 1542 Atlantis
    HDU 4756 Install Air Conditioning
    CodeForces 362E Petya and Pipes
    HDU 4751 Divide Groups
    HDU 3081 Marriage Match II
    UVA 11404 Palindromic Subsequence
    UVALIVE 4256 Salesmen
  • 原文地址:https://www.cnblogs.com/xinde123/p/8776465.html
Copyright © 2011-2022 走看看