• Spring 之Aop实现日志记录


    Aop实现见代码,简单demo实现

     1 package com.idcos.automate.config;
     2 
     3 import com.idcos.automate.dal.auto.dao.dcos.DcosLogDAO;
     4 import com.idcos.automate.dal.auto.dataobject.dcos.DcosLogDO;
     5 import org.aspectj.lang.JoinPoint;
     6 import org.aspectj.lang.ProceedingJoinPoint;
     7 import org.aspectj.lang.annotation.AfterThrowing;
     8 import org.aspectj.lang.annotation.Around;
     9 import org.aspectj.lang.annotation.Aspect;
    10 import org.aspectj.lang.annotation.Pointcut;
    11 import org.slf4j.LoggerFactory;
    12 import org.springframework.beans.factory.annotation.Autowired;
    13 import org.springframework.stereotype.Component;
    14 import org.springframework.web.context.request.RequestContextHolder;
    15 import org.springframework.web.context.request.ServletRequestAttributes;
    16 
    17 import javax.servlet.http.HttpServletRequest;
    18 import java.math.BigDecimal;
    19 import java.util.Arrays;
    20 
    21 /**
    22  * @author GuanBin
    23  * @version LoggerConfig.java, v1 2018/4/24 下午5:34 GuanBin Exp $$
    24  */
    25 @Aspect//切面注解
    26 @Component//@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
    27 public class LoggerConfig {
    28 
    29     @Autowired
    30     private DcosLogDAO dcosLogDAO;
    31 
    32     private final org.slf4j.Logger log = LoggerFactory.getLogger(this.getClass());
    33 
    34 
    35     //定义切点,监听所有的web下的controller
    36     @Pointcut("execution(public * com.idcos.automate.web..*.*(..))")
    37     public void webLog() {
    38     }
    39 
    40     //抛出切点连接中的异常信息
    41     @AfterThrowing(pointcut = "webLog()", throwing = "e")
    42     public void logAfterThrowing(JoinPoint joinPoint, Throwable e) {
    43         log.error("Exception in {}.{}.() cause = {}", joinPoint.getSignature().getDeclaringType(), joinPoint.getSignature().getName(), e.getCause() != null ? e.getCause() : null);
    44     }
    45 
    46     /**
    47      * 在方法前和方法后执行
    48      */
    49     @Around("webLog()")
    50     public void logWebAccessInfo(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
    51 
    52         long start = System.currentTimeMillis();
    53         //JoinPoint、ProceedingJoinPoint提供访问当前被通知方法的目标对象、代理对象、方法参数等数据,其中ProceedingJoinPoint提供访问当前被通知方法的目标对象
    54         // 在环绕Around时使用:
    55         proceedingJoinPoint.proceed();
    56         long end = System.currentTimeMillis();//测试方法执行后的时间是多少
    57         float time =  BigDecimal.valueOf(end-start).divide(BigDecimal.valueOf(1000)).setScale(3, BigDecimal.ROUND_HALF_UP).floatValue();
    58 
    59         ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    60         HttpServletRequest request = attributes.getRequest();
    61 
    62         // 记录下请求内容
    63         log.info("URL : " + request.getRequestURL().toString());//请求的url
    64         log.info("HTTP_METHOD : " + request.getMethod());//请求的方法
    65         log.info("IP : " + request.getRemoteAddr());//请求方的ip
    66         log.info("CLASS_METHOD : " + proceedingJoinPoint.getSignature().getDeclaringTypeName() + "." + proceedingJoinPoint.getSignature().getName());
    67         log.info("ARGS : " + Arrays.toString(proceedingJoinPoint.getArgs()));//请求的参数
    68 
    69         DcosLogDO dcosLogDO = new DcosLogDO();
    70 
    71         dcosLogDO.setClassName(request.getClass().getName());
    72         dcosLogDO.setMethodName(request.getMethod());
    73         dcosLogDO.setUrl(request.getRemoteAddr()+request.getRequestURL());
    74         dcosLogDO.setTime(Float.toString(time));
    75         dcosLogDAO.saveAndFlush(dcosLogDO);
    76     }
    77 
    78 
    79 }

    可参考:

    https://blog.csdn.net/zhengchao1991/article/details/53391244

    作者:guanbin —— 纵码万里千山

    出处:https://www.cnblogs.com/guanbin-529/

    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    ZJOI2019二轮游记
    Luogu P5284 [十二省联考2019]字符串问题
    Luogu P5309 [Ynoi2012]D1T1
    Luogu P5292 [HNOI2019]校园旅行
    LOJ #6052. 「雅礼集训 2017 Day11」DIV
    Luogu P5279 [ZJOI2019]麻将
    LOJ #6060. 「2017 山东一轮集训 Day1 / SDWC2018 Day1」Set
    Luogu P5283 [十二省联考2019]异或粽子
    Luogu P5290 [十二省联考2019]春节十二响
    Luogu P5285 [十二省联考2019]骗分过样例
  • 原文地址:https://www.cnblogs.com/guanbin-529/p/8980709.html
Copyright © 2020-2023  润新知