zoukankan      html  css  js  c++  java
  • Spring Boot2.0之统一处理web请求日志

    试问,你的项目中,如果有几万个方法,你还这么写log.info("name"+name+",age"+age )日志么?low~ 

        所以用AOP呀

    1、首先创建个aop的包(aop的依赖jar包要在pom中搞定),把下面类,贴进去:

    package com.toov5.aop;
    
    import java.util.Enumeration;
    
    import javax.servlet.http.HttpServletRequest;
    
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.annotation.AfterReturning;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.aspectj.lang.annotation.Pointcut;
    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;
    
    @Aspect
    @Component
    public class WebLogAspect {
    
        private static final Logger logger = LoggerFactory.getLogger(WebLogAspect.class);
    
        @Pointcut("execution(public * com.itmayiedu.controller.*.*(..))")
        public void webLog() {
        }
    
        @Before("webLog()")
        public void doBefore(JoinPoint joinPoint) throws Throwable {
            // 接收到请求,记录请求内容
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = attributes.getRequest();
            // 记录下请求内容
            logger.info("URL : " + request.getRequestURL().toString());
            logger.info("HTTP_METHOD : " + request.getMethod());
            logger.info("IP : " + request.getRemoteAddr());
            Enumeration<String> enu = request.getParameterNames();
            while (enu.hasMoreElements()) {
                String name = (String) enu.nextElement();
                logger.info("name:{},value:{}", name, request.getParameter(name));
            }
        }
    
        @AfterReturning(returning = "ret", pointcut = "webLog()")
        public void doAfterReturning(Object ret) throws Throwable {
            // 处理完请求,返回内容
            logger.info("RESPONSE : " + ret);
        }
    }

    2、后端业务逻辑代码:

      

    package com.toov5.ErrorCatch;
    
    
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import lombok.extern.slf4j.Slf4j;
    
    
    
    @RestController //以后经常做微服务异常捕获,要经常用到这个注解
    @Slf4j
    public class ErrorCatch {
        
        @RequestMapping("/getUser")
        public String getUser(int i ){
            int j = 1/i;    
            log.info("成功哈");
            return "返回"+j;
        }
        @RequestMapping("/getMember")
        public String getMember(String name, Integer age){
    //        log.info("name"+name+",age"+age );//如果几万个方法都要写这个么? 所以用aop去统一处理
            return "okokok";
        }
        
        
    }

    3、启动类我做了一个小修改哈:

       

    扩大了一下扫描范围

    现在整个项目的目录结构是这样的:

     启动,请求:

    可以看到控制台:

     小伙伴是不是很简单啊~

     后面会介绍:分布式日志收集系统,也是分布式解决方案的一种~有兴趣有的小伙伴可以自己看下

  • 相关阅读:
    Kubernetes web界面kubernetes-dashboard安装【h】
    K8s+dashboard安装部署【h】
    kubernetes/dashboard Getting Started
    kubernetes/dashboard Creating sample user
    kubernetes安装dashboard步骤 【h】
    Kubernetes Dashboard的安装与坑【h】
    Kubernetes-kubectl The connection to the server localhost:8080 was refused -did you specify
    部署kubernetes1.8.3高可用集群
    linux磁盘读写性能优化
    alias实现命令别名
  • 原文地址:https://www.cnblogs.com/toov5/p/9817833.html
Copyright © 2011-2022 走看看