对比新文件 |
| | |
| | | package com.gk.firework.Config.Log; |
| | | |
| | | import com.alibaba.fastjson.JSONArray; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.gk.firework.Domain.Exception.BusinessException; |
| | | import org.apache.log4j.Logger; |
| | | import org.aspectj.lang.JoinPoint; |
| | | 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; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.Arrays; |
| | | import java.util.Date; |
| | | import java.util.Objects; |
| | | |
| | | @Aspect |
| | | @Component |
| | | public class JsonParamsLogAspect { |
| | | |
| | | private static final Logger logger = Logger.getLogger(JsonParamsLogAspect.class); |
| | | |
| | | @Pointcut("@annotation(com.gk.firework.Domain.Log.JsonParams)") |
| | | public void controllerAspect(){ |
| | | } |
| | | |
| | | @Before(value = "controllerAspect()") |
| | | public void beforeEnterControllerExecute(JoinPoint point) { |
| | | this.logHandle(point); |
| | | } |
| | | |
| | | |
| | | private void logHandle(JoinPoint point) { |
| | | try { |
| | | Object[] args = point.getArgs(); |
| | | if (args.length != 1) throw new BusinessException("系统错误,参数对象只能是一个且固定"); |
| | | |
| | | HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest(); |
| | | String url = request.getRequestURL().toString(); |
| | | SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | String current = formatter.format(new Date()); |
| | | Object arg = args[0]; |
| | | if (arg instanceof JSONArray || arg instanceof JSONObject) { |
| | | logger.info("\n请求时间:" + current + |
| | | "\n请求地址:" + url + |
| | | "\n请求参数:" + arg.toString()); |
| | | } |
| | | } catch (BusinessException e) { |
| | | e.printStackTrace(); |
| | | logger.error(e.getMessage()); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |