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↑ ===========================================
### 如何通过日志工具记录所有运行的方法和接口调用 要实现对程序中所有运行的方法和接口调用日志记录功能,通常会采用以下几种技术手段: #### 1. 使用AOP(面向切面编程) Spring框架中的AOP模块提供了强大的能力来拦截方法执行,并在这些方法前后插入逻辑。这使得我们可以轻松地记录每一个方法的调用及其参数、返回值等信息。 以下是基于Spring AOP的一个简单实现方案[^3]: - **定义自定义注解** 创建一个用于标记需要记录日志的方法的注解`InterfaceLog`。 ```java import java.lang.annotation.*; @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface InterfaceLog { String value() default ""; } ``` - **编写AOP切面类** 利用AOP机制,在目标方法执行前后的特定时间点加入日志记录逻辑。 ```java import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.springframework.stereotype.Component; @Aspect @Component public class LogAspect { @Around("@annotation(InterfaceLog)") public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable { long startTime = System.currentTimeMillis(); Object proceed = joinPoint.proceed(); // 执行原方法 long executionTime = System.currentTimeMillis() - startTime; // 记录日志 StringBuilder sb = new StringBuilder(); sb.append("Method Name: ").append(joinPoint.getSignature().getName()) .append(", Execution Time: ").append(executionTime).append("ms"); System.out.println(sb.toString()); return proceed; // 返回原始结果 } } ``` 此代码片段展示了如何利用AOP捕获带有指定注解的方法调用,并打印它们的名字以及耗时。 #### 2. 工厂方法模式下的日志管理 如果希望支持不同类型的日志存储媒介(如文件或数据库),则可以考虑引入工厂方法模式来动态创建不同的日志处理器对象[^2]。这样不仅增强了系统的可扩展性和维护性,还简化了新增其他类型日志处理方式的过程。 假设我们已经实现了两个具体的Logger子类——FileLogger和DatabaseLogger,则可以通过下面的方式获取对应的实例并完成初始化操作: ```java public abstract class LoggerFactory { protected static final Map<String, Class<? extends AbstractLogger>> loggerMap = new HashMap<>(); static { loggerMap.put("file", FileLogger.class); loggerMap.put("database", DatabaseLogger.class); } public static AbstractLogger getLogger(String type) throws Exception { if (!loggerMap.containsKey(type)) throw new IllegalArgumentException("Invalid logger type."); try { Constructor<?> cons = loggerMap.get(type).getDeclaredConstructor(); return (AbstractLogger) cons.newInstance(); } catch (Exception e) { throw new RuntimeException(e.getMessage(), e); } } } ``` 在这个例子中,当请求某种形式的日志服务时,只需要传入相应的字符串标识符即可获得预设好的具体实现版本。 #### 3. 配合API平台的功能需求 对于像引用描述那样的API服务平台来说[^1],除了常规业务流程外还需要特别关注以下几个方面: - 对外部暴露出来的RESTful风格API做统一监控; - 收集来自客户端的各种访问行为特征作为后续性能评估依据之一; - 提供友好的界面展示给最终使用者查看历史趋势变化曲线图表等等。 综上所述,综合运用以上提到的技术要点就可以构建起一套完整的解决方案体系,从而满足题目提出的关于全面追踪应用程序内部活动轨迹的要求。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值