zoukankan      html  css  js  c++  java
  • SpringBoot全局日志管理(AOP)

    1、在pom.xml中引入aop的jar包

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>

    2、创建WebLogAspect类

    package com.cppdy.log;
    
    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.springframework.stereotype.Component;
    import org.springframework.web.context.request.RequestContextHolder;
    import org.springframework.web.context.request.ServletRequestAttributes;
    
    @Aspect
    @Component
    public class WebLogAspect {
        
        @Pointcut("execution(public * com.cppdy.controller..*.*(..))")
        public void webLog() {
    
        }
    
        @Before("webLog()")
        public void doBefore(JoinPoint joinPoint) throws Throwable {
            // 接收到请求,记录请求内容
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = attributes.getRequest();
            // 记录下请求内容
            System.out.println("URL:" + request.getRequestURL().toString());
            System.out.println("HTTP_METHOD:" + request.getMethod());
            System.out.println("IP:" + request.getRemoteAddr());
            Enumeration<String> enu = request.getParameterNames();
            while (enu.hasMoreElements()) {
                String name = enu.nextElement();
                System.out.println("name:" + name + ",value:" + request.getParameter(name));
            }
        }
        
        @AfterReturning(returning="ret",pointcut="webLog()")
        public void doAfterReturning(Object ret)throws Throwable {
            //处理完请求,返回内容
            System.out.println("RESPONSE:"+ret);
        }
    }

    3、访问login方法测试全局日志管理功能

  • 相关阅读:
    apache wicket 7.X让html回归webapp文件夹下
    HDU 4050 wolf5x (概率DP 求期望)
    struts2和数据库模糊查询
    codeforces 453A Little Pony and Expected Maximum 最大值期望
    挖坑
    BZOJ1430: 小猴打架
    BZOJ1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚
    1645: [Usaco2007 Open]City Horizon 城市地平线
    POJ1741 Tree
    CH Round #53-数据备份
  • 原文地址:https://www.cnblogs.com/jiefu/p/10051939.html
Copyright © 2011-2022 走看看