日志与异常处理

@Slf4j
如果不想每次都写private final Logger logger = LoggerFactory.getLogger(XXX.class); 可以用注解@Slf4j

日志

  • org.slf4j.Logger
  • private final static Logger logger= LoggerFactory.getLogger(HttpAspect.class);
  • info方法
  • error方法等

获取URL method IP 类方法 参数

@Before("log()")
    public void dolog(JoinPoint joinPoint){
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        //url
        logger.info("url={}",request.getRequestURL());

        //method
        logger.info("method={}",request.getMethod());

        //ip
        logger.info("ip={}",request.getRemoteAddr());

        //类方法
        logger.info("classMethod={}",joinPoint.getSignature().getDeclaringTypeName()+"."+joinPoint.getSignature().getName());

        //参数
        logger.info("args={}",joinPoint.getArgs());
    }

统一异常处理

  • 规范JSON格式
  • 新建一个Result的类,建立code,data等数据域
  • 新建工具类ResultUtil,编写静态方法:Error,Success等