栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

项目AOP处理请求日志

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

项目AOP处理请求日志

项目AOP处理请求日志 单个请求

功能描述:在进入请求前打印日志及参数,请求执行完成后打印响应结果。
为要打印日志的请求定义注解:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface RequestLog {
    
    String value() default "";
}

使用AOP解析每个使用了该注解的请求:

@Slf4j
@Aspect
@Component
public class RequestLogResolver {

    @Pointcut("@annotation(com.xxx.xxx.common.aspect.RequestLog)")
    private void pointCut() {
    }

    @Around("pointCut()")
    public Object beforeRequestLog(ProceedingJoinPoint pjp) throws Throwable {
        MethodSignature signature = (MethodSignature) pjp.getSignature();
        Method method = signature.getMethod();
        Annotation[] annotations = method.getAnnotations();
        RequestLog requestLogAnnotation = null;
        for (Annotation annotation : annotations) {
            if (annotation.annotationType().getSimpleName().equals(RequestLog.class.getSimpleName())) {
                requestLogAnnotation = (RequestLog) annotation;
                break;
            }
        }
        String requestName = "";
        if (requestLogAnnotation != null) {
            requestName = requestLogAnnotation.value();
        }
        StringBuilder parameterPlaceHolder = new StringBuilder();
        Object[] args = pjp.getArgs();
        int length = args.length;
        if (length > 0) {
            for (int i = 0; i < length; i++) {
                parameterPlaceHolder.append("参数 ").append(i + 1).append(":{}  ");
            }
            log.info("开始执行【" + requestName + "】请求,请求参数为:" + parameterPlaceHolder, args);
        } else {
            log.info("开始执行【" + requestName + "】请求,请求参数为空");
        }
        Object result = pjp.proceed(args);
        log.info("结束执行【" + requestName + "】请求,请求结果为:{}", result);
        return result;
    }

}
微服务关联请求

(下面代码是基于Feign写的,服务间调用采用其它方式的可以参考下面的写法)
功能描述:在进入请求前打印请求参数,如果整个请求内有访问其它服务,其它服务会基于同一个请求ID,打印日志,请求结束后打印响应结果。服务内所有请求都有同一个请求ID,便于定位问题。

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface RequestLog {
    
    String value() default "";

    
    boolean extendsRequestId() default true;
}

@Slf4j
@Aspect
@Component
public class RequestLogResolver {

    public static final String REQUEST_LOG_ID_NAME = "NEW_COIN_REQUEST_LOG_ID";

    @Pointcut("@annotation(com.xxx.common.web.aspect.RequestLog)")
    private void pointCut() {
    }

    @Around("pointCut()")
    public Object beforeRequestLog(ProceedingJoinPoint pjp) throws Throwable {
        MethodSignature signature = (MethodSignature) pjp.getSignature();
        Method method = signature.getMethod();
        Annotation[] annotations = method.getAnnotations();
        RequestLog requestLogAnnotation = null;
        for (Annotation annotation : annotations) {
            if (annotation.annotationType().getSimpleName().equals(RequestLog.class.getSimpleName())) {
                requestLogAnnotation = (RequestLog) annotation;
                break;
            }
        }
        String requestName = "";
        boolean extendsRequestId = true;
        if (requestLogAnnotation != null) {
            requestName = requestLogAnnotation.value();
            extendsRequestId = requestLogAnnotation.extendsRequestId();
        }
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        String requestId = null;
        if (extendsRequestId) {
            // 继承RequestId,从Header中取值
            requestId =  request.getHeader(REQUEST_LOG_ID_NAME);
        }

        if (requestId == null) {
            requestId = "REQUEST_" + UUIDUtils.uuid();
        }
        HttpSession session = request.getSession();
        session.setAttribute(REQUEST_LOG_ID_NAME, requestId);

        StringBuilder parameterPlaceHolder = new StringBuilder();
        Object[] args = pjp.getArgs();
        int length = args.length;
        if (length > 0) {
            for (int i = 0; i < length; i++) {
                parameterPlaceHolder.append("参数 ").append(i + 1).append(":{}  ");
            }
            log.info("开始执行【" + requestName + "】请求【{}】,请求参数为:" + parameterPlaceHolder, requestId, args);
        } else {
            log.info("开始执行【" + requestName + "】请求【{}】,请求参数为空");
        }
        Object result = pjp.proceed(args);
        log.info("结束执行【" + requestName + "】请求【{}】,请求结果为:{}", requestId, result);
        // 请求结束后,清除请求ID
        session.removeAttribute(REQUEST_LOG_ID_NAME);
        return result;
    }

}
@Slf4j
@Configuration
public class FeignConfiguration implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpSession session = attributes.getRequest().getSession();
        String requestId = (String) session.getAttribute(RequestLogResolver.REQUEST_LOG_ID_NAME);
        if (StringUtils.isNotEmpty(requestId)) {
            log.debug("Feign调用设置请求ID,{}", requestId);
            requestTemplate.header(RequestLogResolver.REQUEST_LOG_ID_NAME, requestId);
        }
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/678390.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号