/**
* 请求的日志处理 ,写完这个在主配置文件中设置一下日志输入的位置即可 ,AOP思想
* Created by Donghua.Chen on 2018/4/28.
*/
@Aspect
@Component
public class WebLogAspect {
// @Autowired
// private LogService logService;
private static Logger LOGGER = LoggerFactory.getLogger(WebLogAspect.class);
ThreadLocal<Long> startTime = new ThreadLocal<>();
@Pointcut("execution(public * cn.luischen.controller..*.*(..))")
public void webLog(){}
@Before("webLog()")
public void doBefore(JoinPoint joinPoint){
startTime.set(System.currentTimeMillis()); //设置当前时间
//接收到请求,记录请求内容,获得切点方法中执行时候的各种参数
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
HttpSession session = request.getSession();
// 记录下请求内容 (一般就记录这几个)还可以把这些封装到一个对象中
LOGGER.info("URL : " + request.getRequestURL().toString());
LOGGER.info("HTTP_METHOD : " + request.getMethod());
LOGGER.info("IP : " + request.getRemoteAddr());
LOGGER.info("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
LOGGER.info("ARGS : " + Arrays.toString(joinPoint.getArgs()));//获得切点方法中传递的参数(可能有很多)所以是一个Arrays
}
//要获得方法返回值可以用下面这个(一般用于API接口文档Swegger中)
@AfterReturning(returning = "ret", pointcut = "webLog()")//环绕通知
//这个方法注解可以获得切点中方法的返回值,ret来接收到
public void doAfterReturning(Object ret) throws Throwable {
// 处理完请求,返回内容
LOGGER.info("RESPONSE : " + ret);
LOGGER.info("SPEND TIME : " + (System.currentTimeMillis() - startTime.get()));
startTime.remove();//用完之后记得清除,不然可能导致内存泄露;
}
}
AOP之日志分析
最新推荐文章于 2022-03-21 17:31:59 发布