AspectJ统一打印接口访问日志

该博客介绍了如何使用Spring Boot的AOP(面向切面编程)来实现接口调用的日志记录。通过在接口方法上添加自定义注解`@NetLog`,可以详细记录请求的URL、HTTP头、参数、响应状态等信息,便于系统操作跟踪和问题排查。示例展示了日志输出的内容和格式。

pom中添加依赖

 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-aop</artifactId>
 </dependency>

创建注解

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
@Documented
public @interface NetLog {
    String desc() default "";
}

编写切面逻辑

@Aspect
@Component
public class NetLogAspect {

    private final static Logger logger = LoggerFactory.getLogger(NetLogAspect.class);
    private static final String LINE_SEPARATOR = System.lineSeparator();//换行符

    @Pointcut("@annotation(cc.catface.common.aspect.NetLog)")
    public void netLog() {
    }

    @Before("netLog()")
    public void doBefore(JoinPoint joinPoint) throws Throwable {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes == null) return;
        HttpServletRequest request = attributes.getRequest();
        //注解的desc描述信息
        String desc = getAspectLogDescription(joinPoint);
        //请求头
        StringBuilder reqHeader = new StringBuilder();
        Enumeration<String> reqHeadNames = request.getHeaderNames();
        while (reqHeadNames.hasMoreElements()) {
            String reqHeadName = reqHeadNames.nextElement();
            reqHeader.append(reqHeadName).append(":").append(request.getHeader(reqHeadName)).append(", ");
        }
        //get参数
        String queryString = (request.getQueryString() == null) ? "" : URLDecoder.decode(request.getQueryString());
        logger.info("========================================== ↓Start↓ ==========================================");
        logger.info("METHOD & URL & IP     : {} {} {}", request.getMethod(), request.getRequestURL().toString(), request.getRemoteAddr());
        logger.info("DESC & CLASS.METHOD   : {} {}.{}", desc, joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName());
        logger.info("REQ-HEADER            : {}", reqHeader);
        logger.info("REQ-PARAMS(QUERY)     : {}", queryString);
        logger.info("REQ-PARAMS(BODY)      : {}", JSON.toJSON(joinPoint.getArgs()));
        logger.info("REQ-SESSION-ID        : {}", request.getSession().getId());

        HttpServletResponse response = attributes.getResponse();
        if (response == null) return;
        //响应头
        StringBuilder resHeader = new StringBuilder();
        Collection<String> resHeadNames = response.getHeaderNames();
        resHeadNames.forEach(header -> resHeader.append(header).append(":").append(response.getHeader(header)).append(", "));
        logger.info("RES-CODE              : {}", response.getStatus());
        logger.info("RES-HEADER            : {}", resHeader);
    }


    @Around("netLog()")
    public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        long startTime = System.currentTimeMillis();
        Object result = proceedingJoinPoint.proceed();
        logger.info("RES                   : {}", JSON.toJSON(result));
        logger.info("TIME-CONSUMING        : {}ms", System.currentTimeMillis() - startTime);
        logger.info("=========================================== ↑End↑ ===========================================" + LINE_SEPARATOR);
        return result;
    }

    @After("netLog()")
    public void doAfter() throws Throwable {
    }

    /* 获取切面注解的desc描述 */
    public String getAspectLogDescription(JoinPoint joinPoint) throws Exception {
        String targetName = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();
        Object[] arguments = joinPoint.getArgs();
        Class targetClass = Class.forName(targetName);
        Method[] methods = targetClass.getMethods();
        StringBuilder description = new StringBuilder("");
        for (Method method : methods) {
            if (method.getName().equals(methodName)) {
                Class[] clz = method.getParameterTypes();
                if (clz.length == arguments.length) {
                    description.append(method.getAnnotation(NetLog.class).desc());
                    break;
                }
            }
        }
        return description.toString();
    }
}

接口方法上使用注解

主要是@NetLog(desc = "")

@NetLog(desc = "查询号源")
@ApiOperationSupport(order = 40)
@ApiOperation(value = "查询号源[已开发]")
@ApiResponses(@ApiResponse(code = 200, message = "Success", response = GetNumbersOutputDTO.class))
@PostMapping("getNumbers")

示例日志

其中==>和<==为SQL日志

[INFO ] 2022-09-14 17:05:32 800 ========================================== ↓Start↓ ==========================================
[INFO ] 2022-09-14 17:05:32 801 METHOD & URL & IP     : POST http://localhost:9001/outpatient/getNumbers 0:0:0:0:0:0:0:1
[INFO ] 2022-09-14 17:05:32 801 DESC & CLASS.METHOD   : 查询号源 cc.catface.api.biz.OutpatientController.getNumbers
[INFO ] 2022-09-14 17:05:32 801 REQ-HEADER            : content-type:application/json, user-agent:PostmanRuntime/7.29.2, accept:*/*, postman-token:77d948fd-3e17-4bfb-8bbf-e79acde16fb2, host:localhost:9001, accept-encoding:gzip, deflate, br, connection:keep-alive, content-length:28, cookie:JSESSIONID=014C4A36205A8D29A89D8BAB5F2EA71D, 
[INFO ] 2022-09-14 17:05:32 801 REQ-PARAMS(QUERY)     : aa=啊啊
[INFO ] 2022-09-14 17:05:32 802 REQ-PARAMS(BODY)      : [{"date":"2021/09/19"}]
[INFO ] 2022-09-14 17:05:32 802 REQ-SESSION-ID        : 014C4A36205A8D29A89D8BAB5F2EA71D
[INFO ] 2022-09-14 17:05:32 802 RES-CODE              : 200
[INFO ] 2022-09-14 17:05:32 802 RES-HEADER            : Vary:Origin, Vary:Origin, Vary:Origin, 
[DEBUG] 2022-09-14 17:05:32 808 ==>  Preparing: SELECT CLINIC_DATE AS clinicDate,CLINIC_LABEL AS clinicLabel,TIME_DESC AS timeDesc,REGISTRATION_LIMITS AS registrationLimits,APPOINTMENT_LIMITS AS appointmentLimits,CURRENT_NO AS currentNo,REGISTRATION_NUM AS registrationNum,APPOINTMENT_NUM AS appointmentNum,REGIST_PRICE AS registPrice,DOCTOR FROM clinic_for_regist WHERE (CLINIC_DATE = to_date(?, 'yyyy/MM/dd'))
[DEBUG] 2022-09-14 17:05:32 809 ==> Parameters: 2021/09/19(String)
[DEBUG] 2022-09-14 17:05:32 820 <==      Total: 44
[INFO ] 2022-09-14 17:05:32 836 RES                   : {"code":"0","data":[{"clinicDate":"2021/09/19","clinicLabel":"产科门诊","doctor":"王兴娣","registPrice":"1.00","timeDesc":"白天"}],"message":"success"}
[INFO ] 2022-09-14 17:05:32 837 TIME-CONSUMING        : 37ms
[INFO ] 2022-09-14 17:05:32 837 =========================================== ↑End↑ ===========================================
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值