AOP实现对日志的打印

项目场景:

日志的打印,不用再去Log.info了。


解决思路:

1、通过Zuul来获取到请求信息,实现对请求信息的打印。
通过zuul来获取的时候报错了。而且有一些接口是不会被zuul进行拦截的。
2、通过AOP对所有的Controller进行请求的信息和返回的信息打印。
环绕获取到Controller。通过一下几种方法:
https://blog.csdn.net/qq_36951116/article/details/79172485


3、获取请求的param信息和JSON信息 请求param和请求application/json格式的获取不相同。request只能被获取一次,由于Controller中spring获取了一次了,所以不能在request中获取了。可以通过如下方法获取: https://blog.csdn.net/weixin_42382291/article/details/109511620 但是通过上面这个方法获取会造成延迟过长和创建的静态变量越来越大。SpringAO提供了获取参数的方法。如下: https://blog.csdn.net/cs373616511/article/details/107535480 通过这种可以很顺利的获取到相关信息。需要注意的是:通过此方法获取到的args,会获取Controller方法中的HttpServerRequset。而他不能进行JSON.toJsonString。 4、优化。 由于这个仅仅是记录日志,所以它的任何错误都不应该导致不能往下进行,所以要进行捕获错误,不能防止他下一步进行

代码:

package com.sinosoft.app.project.aop;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sinosoft.app.project.aop.LogPojo;
import io.swagger.annotations.ApiOperation;
import org.apache.http.protocol.HTTP;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ObjectUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.util.*;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import static com.sinosoft.app.base.utils.hutool.HutoolUtil.log;


/**
 * @description 拦截所有控制器的返回,记录响应报文
 * @author JYJ
 * @date 2021-03-17 上午11:52:26
 */
@Aspect
@Configuration
public class LogAspect {


    public LogAspect() {
    }

    @Pointcut("execution(@(org.springframework.web.bind.annotation.RequestMapping || org.springframework.web.bind.annotation.PostMapping) * *(..))")
    public void pointCutMethod() {
    }

    // 声明环绕通知
    @Around("pointCutMethod()")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
            Long startTime = System.currentTimeMillis();
            Object ret = null;
        try {
            ApiOperation apiOperation = ((MethodSignature) pjp.getSignature()).getMethod().getAnnotation(ApiOperation.class);
            // 获取request对象
            RequestAttributes ra = RequestContextHolder.getRequestAttributes();
            ServletRequestAttributes sra = (ServletRequestAttributes) ra;
            HttpServletRequest request = sra.getRequest();
            Map<String, Object> params = showParams(request);
            log.info("【请求参数】:{}", JSON.toJSONString(params));
//        log.info("请求参数02222:{}",JSON.toJSONString(request.getParameterMap()));
            //请求体,只有params为空的时候才有可能会有Body
            if (ObjectUtils.isEmpty(params)) {
                try {
                    Object[] args = pjp.getArgs();
                    String body = JSON.toJSONString(ObjectUtils.isEmpty(args) ? "" : args[0]);
                    log.info("【请求JSON】:{}", body);
                } catch (Exception e) {
                    log.info("【请求JSON】:获取失败");
                }
            }
            log.info("【请求URL】:{}", request.getRequestURL());
            log.info("【请求方法名】:{}", pjp.getSignature().getName());
        }catch (Exception e){
            log.info("打印日志失败,错误原因:{}",e.getMessage());
        }finally {
            //执行代码
            ret = pjp.proceed();
        }
        //结束
        try {
            Long endTime = System.currentTimeMillis();
            log.info("响应数据耗时:{}",endTime-startTime);
            log.info("响应数据:{}",JSON.toJSONString(ret));
        }catch (Exception e){
            log.info("打印日志失败,错误原因:{}",e.getMessage());
        }finally {
            return ret;
        }
    }


    public static Map<String,Object> showParams(HttpServletRequest request) {
        Map<String,Object> map = new HashMap<String,Object>();
        Enumeration paramNames = request.getParameterNames();
        while (paramNames.hasMoreElements()) {
            String paramName = (String) paramNames.nextElement();

            String[] paramValues = request.getParameterValues(paramName);
            if (paramValues.length >0) {
                String paramValue = paramValues[0];
                if (paramValue.length() != 0) {
                    map.put(paramName, paramValue);
                }
            }
        }
        return map;
    }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值