理由aop保存日志,区分路径式和body式

package com.quxiao.log;

import cn.hutool.json.JSONUtil;
import com.quxiao.util.IPUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.connector.RequestFacade;
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.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.method.annotation.ExtendedServletRequestDataBinder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponseWrapper;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 082804
 * @version 1.0
 * @description: 全局日志处理
 * @date 2022/11/24 14:59
 */
@Component
@Aspect
@RequiredArgsConstructor
@Slf4j
public class GlobalLogAdvice {


    /**
     * 全局日志切点定义,标有RequestMapping类的所有方法都进行切入
     */
    @Pointcut("@within(org.springframework.web.bind.annotation.RequestMapping)")
    public void pointCut() {
    }

    @Around("pointCut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
        GetMapping getMapping = method.getAnnotation(GetMapping.class);
        Object[] args = joinPoint.getArgs();
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String[] argNames = ((MethodSignature) joinPoint.getSignature()).getParameterNames();
        if (getMapping != null) {
            //路径方式
            getMethodLog(request);
            return joinPoint.proceed(args);
        }
        Long start = System.currentTimeMillis();
        Long end = null;
        //获取方法参数信息
        RequestMapping api = joinPoint.getTarget().getClass().getAnnotation(RequestMapping.class);

        Map paramMap = new HashMap();
        for (int i = 0; i < args.length; i++) {
            if (!(args[i] instanceof ExtendedServletRequestDataBinder) && !(args[i] instanceof HttpServletResponseWrapper) && !(args[i] instanceof MultipartFile)) {
                paramMap.put(argNames[i], args[i]);
            }
        }
        //body类型参数调用
        bodyMethod(method, request, api, paramMap);
        Object result = null;
        try {
            result = joinPoint.proceed(args);
            end = System.currentTimeMillis();
        } catch (Throwable throwable) {
            end = System.currentTimeMillis();
            //异常通知
            throw throwable;
        } finally {
            Long time = end - start;
            System.out.println(time);
        }
        return result;
    }

    private static void bodyMethod(Method method, HttpServletRequest request, RequestMapping api, Map paramMap) {
        System.out.println(api.value()[0]);
        System.out.println(method.getName());
        System.out.println(JSONUtil.toJsonStr(paramMap));
        System.out.println(IPUtils.getIpAddr(request));
    }

    private static void getMethodLog(HttpServletRequest request) {
        String userIp = IPUtils.getIpAddr(request);
        RequestFacade facade = (RequestFacade) request;
        String scheme = facade.getScheme();
        String ip = facade.getServerName();
        int port = facade.getServerPort();
        String queryName = facade.getRequestURI();
        String queryString = facade.getQueryString();
        System.out.println("用户id:" + userIp);
        System.out.println(scheme + "://" + ip + ":" + port + queryName + "?" + queryString);
    }

}

获取调用者ip:Spring Boot 获取接口调用者的IP_streamobserver 调用方ip-CSDN博客

你可能感兴趣的:(项目小技巧,java)