前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java AOP实现接口请求日志打印

Java AOP实现接口请求日志打印

作者头像
赵哥窟
发布2022-05-13 08:09:23
8150
发布2022-05-13 08:09:23
举报
文章被收录于专栏:日常技术分享

在学习Spring-cloud框架的时候,正好用到了打印请求参数,Java还好有Aop,不用在请求的时候和返回的时候打日志,统一由Aop来实现,实现很简单

代码语言:javascript
复制
package com.tansun.springcloud.project.b.web.config;

import com.alibaba.fastjson.JSON;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Component
@Aspect
public class RequestLogAspect {

    private static final Logger log = LoggerFactory.getLogger(RequestLogAspect.class);

    /**
     * 切入点
     */
    @Pointcut("execution(* com.demo.springcloud.project.b.web.controller..*(..)) ")
    public void entryPoint() {
        // 无需内容
    }

    @Before("entryPoint()")
    public void doBefore(JoinPoint joinPoint) {
        try {
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = attributes.getRequest();

            String className = joinPoint.getTarget().getClass().getName();
            String methodName = joinPoint.getSignature().getName();
            Object[] parameters = joinPoint.getArgs();
            log.info("==================接口请求日志开始==================");
            log.info("URL:"+request.getRequestURL().toString()+"\n"
                    +"IP:"+request.getRemoteAddr()+"\n"
                    +"HTTP Method:"+request.getMethod()+"\n"
                    +"类名:" + className+"\n"
                    + "方法名:" + methodName+"\n"
                    + "请求参数:" + JSON.toJSONString(parameters));
            log.info("==================接口请求日志结束==================");
        } catch (Throwable e) {
            log.info("around " + joinPoint + " with exception : " + e.getMessage());
        }

    }

    @Around("entryPoint()")
    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {

        String className = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();

        long startTime = System.currentTimeMillis();
        Object result = joinPoint.proceed();
        long time = System.currentTimeMillis() - startTime;

        log.info("==================接口返回日志开始==================");
        log.info(
                "类名:" + className+"\n"
                + "方法名:" + methodName+"\n"
                + "返回结果:" + JSON.toJSONString(result)+"\n"
                + "方法执行耗时:"+time+"ms"
        );
        log.info("==================接口返回日志结束==================");

        return result;
    }

    @AfterThrowing(pointcut = "entryPoint()", throwing = "e")
    public void doAfterThrowing(JoinPoint joinPoint, Throwable e) {
        // 通过request获取登陆用户信息
        // HttpServletRequest request = ((ServletRequestAttributes)
        // RequestContextHolder.getRequestAttributes()).getRequest();
        try {
            String className = joinPoint.getTarget().getClass().getName();
            String methodName = joinPoint.getSignature().getName();
            Object[] parameters = joinPoint.getArgs();

            log.info("异常方法:" + className + "." + methodName + "();参数:" + JSON.toJSONString(parameters));
            log.info("异常信息:" + e.getMessage());
        } catch (Exception ex) {
            log.error("异常信息:{}", ex.getMessage());
        }
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-05-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档