Spring中用切点记录日志@Aspect

  

@Aspect
@Component
public class LogAspect {
private static final Logger log = LoggerFactory.getLogger(LogAspect.class);


@Pointcut("execution(public * com.example.demo.controller..*.*(..))") //切点
public void webLog() {
}

/**
*
* @param joinPoint
*/
@Before("webLog()") //切点之前执行
public void doBefore(JoinPoint joinPoint) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();

log.info("请求的地址" + String.valueOf(request.getRequestURL()));
log.info("请求的IP" + String.valueOf(request.getRemoteAddr()));
}

@AfterReturning(returning = "ret", pointcut = "webLog()") //切点后返回值
public void doAfterReturning(Object ret) {
log.info(ret.toString());
}


}
上一篇:51job招聘爬虫&数据分析


下一篇:关于Flume-ng那些事(三)