zoukankan      html  css  js  c++  java
  • AOP打印请求日志,打印返回值

    @Aspect
    // 申明是个spring管理的bean
    @Component
    @Slf4j
    public class LogAspectServiceApi {
        private JSONObject jsonObject = new JSONObject();
    
        // 申明一个切点 里面是 execution表达式
        @Pointcut("execution(public * com.itmayiedu.api.service.*.*(..))")
        private void controllerAspect() {
        }
    
        // 请求method前打印内容
        @Before(value = "controllerAspect()")
        public void methodBefore(JoinPoint joinPoint) {
            ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
                    .getRequestAttributes();
            HttpServletRequest request = requestAttributes.getRequest();
            log.info("===============请求内容===============");
            try {
                // 打印请求内容
                log.info("请求地址:" + request.getRequestURL().toString());
                log.info("请求方式:" + request.getMethod());
                log.info("请求类方法:" + joinPoint.getSignature());
                log.info("请求类方法参数:" + JSONObject.toJSONString(joinPoint.getArgs(),true);
          } catch (Exception e) { log.error("###LogAspectServiceApi.class methodBefore() ### ERROR:", e); } log.info("===============请求内容==============="); } 

    // 在方法执行完结后打印返回内容
    @AfterReturning(returning = "o", pointcut = "controllerAspect()")
    public void methodAfterReturing(Object o) {
    log.info(
    "--------------返回内容----------------");
    try {
    log.info(
    "Response内容:" + jsonObject.toJSONString(o));
    }
    catch (Exception e) {
    log.error(
    "###LogAspectServiceApi.class methodAfterReturing() ### ERROR:", e);
    } log.info(
    "--------------返回内容----------------");
    }
    }
  • 相关阅读:
    Linux Shell脚本详细教程
    linux下错误代码E212: Can't open file for writing
    github仓库的基本使用-创建、上传文件、删除
    -bash: ifconfig: command not found解决办法
    Xshell能ping通但连不上CentOS 7
    devtools和vuex mutations
    Google Chrome谷歌浏览器安装devtools
    Vuex状态管理模式
    vue Promise all
    vue Promise链式调用
  • 原文地址:https://www.cnblogs.com/brxHqs/p/10335582.html
Copyright © 2011-2022 走看看