zoukankan      html  css  js  c++  java
  • springboot日志拦截器

    import com.alibaba.fastjson.JSONObject;
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.annotation.*;
    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;
    import javax.servlet.http.HttpServletRequest;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    @Aspect
    @Component
    public class LogHttpAspect {
    private Logger logger = LoggerFactory.getLogger(LogHttpAspect.class);
    //execution 后面填写拦截的controller的路径
    @Pointcut("execution(public * com.xxx.xxx.controller.*.*(..))")
    public void log(){
    }
    private long start;

    @Before("log()")
    public void doBefore(JoinPoint joinPoint){
    start = System.currentTimeMillis();
    logger.info("api request start "+(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date())));
    ServletRequestAttributes attributes= (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    HttpServletRequest request=attributes.getRequest();
    logger.info("url={}",request.getRequestURI());
    logger.info("method={}",request.getMethod());
    logger.info("ip={}",request.getRemoteAddr());

    if(joinPoint!=null) {
    logger.info("class_method={}",joinPoint.getSignature().getDeclaringTypeName()+"."+ joinPoint.getSignature().getName());
    logger.info("args={}", JSONObject.toJSON(joinPoint.getArgs()));
    }
    }

    /*@After("log()")
    public void doAfter(){
    }*/
    //获取接口返回的内容
    @AfterReturning(returning = "object",pointcut = "log()")
    public void doAfterReturining(Object object){
    long end = System.currentTimeMillis();
    logger.info("time={}",(end-start)+"ms");
    if(object==null){
    logger.info("returning={}","");
    }else {
    logger.info("returning={}",JSONObject.toJSON(object).toString());
    }
    }

    }

  • 相关阅读:
    一道有意思的面试算法题
    你真的了解回流和重绘吗
    一道面试题引起的思考
    手把手教你实现一个引导动画
    你可能不知道的setInterval的坑
    不到50行代码实现一个能对请求并发数做限制的通用RequestDecorator
    使用装饰者模式做有趣的事情
    广告
    vue运行svg文件
    el-table表格样式设置方法 :cell-class-name
  • 原文地址:https://www.cnblogs.com/shoshana-kong/p/14290306.html
Copyright © 2011-2022 走看看