SpringBoot AOP 实现请求日志记录

引入依赖


            aspectj
            aspectjweaver
            1.5.4
        

监控类代码,目前只做了控制台输出,后续自己切换到文本或者数据库中

package com.java.core.web.config;

import com.alibaba.fastjson.JSONObject;
import com.java.core.core.vo.HttpResult;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
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;

//处理AOP切面请求日志
@Slf4j
@Component
@Aspect
public class DBLogConfig {

    //方法执行前-要监控的请求包地址
    @Before("execution(public * com.java.core.web.contrller.*.*(..))")
    public void requestBefore(JoinPoint joinPoint) throws Exception {

        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        StringBuilder requestLog = new StringBuilder();
        Signature signature = joinPoint.getSignature();
        requestLog.append("请求信息:").append("URL = {").append(request.getRequestURI()).append("},\t")
                .append("请求方式 = {").append(request.getMethod()).append("},\t")
                .append("请求IP = {").append(request.getRemoteAddr()).append("},\t")
                .append("类方法 = {").append(signature.getDeclaringTypeName()).append(".")
                .append(signature.getName()).append("},\t");

        // 处理请求参数
        String[] paramNames = ((MethodSignature) signature).getParameterNames();
        Object[] paramValues = joinPoint.getArgs();
        int paramLength = null == paramNames ? 0 : paramNames.length;
        if (paramLength == 0) {
            requestLog.append("请求参数 = {} ");
        } else {
            requestLog.append("请求参数 = [");
            for (int i = 0; i < paramLength - 1; i++) {
                requestLog.append(paramNames[i]).append("=").append(JSONObject.toJSONString(paramValues[i])).append(",");
            }
            requestLog.append(paramNames[paramLength - 1]).append("=").append(JSONObject.toJSONString(paramValues[paramLength - 1])).append("]");
        }

        log.info(requestLog.toString());
    }


    //方法执行后-要监控的请求包地址
    @AfterReturning(returning = "result", pointcut = "execution(public * com.java.core.web.contrller.*.*(..))")
    public void requestAfter(HttpResult result) throws Exception {
        log.info("请求结果:" +JSONObject.toJSONString(result));
    }
}

请求后的输出情况

 监控多个类,以及返回的时候输出请求记录

//方法执行后-要监控的请求包地址
    @AfterReturning(returning = "result", pointcut = "execution(public * com.java.core.web.contrller.*.*(..))"
            +"|| execution(public * com.java.core.web.contrller.master.*.*(..))")
    public void requestAfter(JoinPoint joinPoint,HttpResult result) throws Exception {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String token = request.getHeader(JwtTokenUtils.AUTHORITIES);
        String loginName=JwtTokenUtils.getUsernameFromToken(token);
        log.info("请求结果:" +JSONObject.toJSONString(result));
    }

你可能感兴趣的:(SpringBoot,AOP)