• AOP统一处理Web请求日志


       <!--aop-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-aop</artifactId>
            </dependency>
    
    @Aspect
    @Component
    public class WebLogAspect {
        private Logger logger = Logger.getLogger(getClass().toString());
    
        @Pointcut("execution(public * shuju.controller..*.*(..))")
        public void webLog() {
        }
    
        @Before("webLog()")
        public void doBefore(JoinPoint joinPoint) {
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = attributes.getRequest();
            logger.info("--------------request------------");
            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 = enu.nextElement();
                logger.info("name:" + name + "value:" + request.getParameter(name));
            }
        }
    
        @AfterReturning(returning = "ret",pointcut = "webLog()")
        public void doAfterReturning(Object ret){
            logger.info("--------------response------------");
            logger.info("RESPONSE:"+ret);
        }
    }
    
  • 相关阅读:
    数据库基础
    Junit单元测试、反射与注解
    Stream流
    Java IO所用类
    字符集
    Lambda表达式
    【jdk1.8源码分析】LinkedHashMap
    线性表
    【jdk1.8源码分析】ArrayList
    java学习血泪史
  • 原文地址:https://www.cnblogs.com/fly-book/p/10642447.html
Copyright © 2020-2023  润新知