直接上代码吧:
/**
*Aop运用:记录接口调用日志
* @author 咸蛋超人
*/
@Component
@Aspect
@Slf4j
public class ActApiLogAop {
//实际项目路径
@Around("execution(* com.demo.rest.*.*(..)))")
public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
StringBuilder name = null;
//Spring计时器:Stopwatch可方便的对程序部分代码进行计时(ms级别),适用于同步单线程代码块。
Stopwatch stopwatch = Stopwatch.createStarted();
try {
name = new StringBuilder(pjp.getTarget().getClass().getSimpleName()).append(":").append(pjp.getSignature().getName());
Object o = pjp.proceed();
return o;
} catch (Throwable throwable) {
log.error("接口:" + name + "抛异常",throwable);
throw throwable;
} finally {
log.info("接口:{},耗时:{}ms",
name,
stopwatch.elapsed(TimeUnit.MILLISECONDS));
}
}
}
来源:oschina
链接:https://my.oschina.net/u/4247262/blog/3195967