AOP统一日志管理
@Aspect
@Component
public class ControllerLogConfig {
@Autowired
LogManager Log;
@Pointcut("execution(* com.controller..*.*(..))")
private void controllerAspect() {
}
@Around("controllerAspect()")
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
long begin = System.currentTimeMillis();
RequestAttributes ra = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes sra = (ServletRequestAttributes) ra;
if (ra == null || sra == null) {
return joinPoint.proceed();
}
HttpServletRequest request = sra.getRequest();
ActionLog logContent = new ActionLog();
logContent.setTransactionId(request.getHeader("id"));
logContent.setRequest(JsonUtils.objectToJson(joinPoint.getArgs()));
Object result = null;
result = joinPoint.proceed();
logContent.setResponse(JsonUtils.objectToJson(result));
long costTime = System.currentTimeMillis() - begin;
logContent.setCostTime(String.valueOf(costTime));
Log.log(logContent);
return result;
}
@AfterReturning("controllerAspect()")
public void afterReturning(JoinPoint joinPoint) {
}
@AfterThrowing(pointcut = "controllerAspect()", throwing = "e")
public void afterThrowing(JoinPoint joinPoint, Throwable e) {
long begin = System.currentTimeMillis();
RequestAttributes ra = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes sra = (ServletRequestAttributes) ra;
if (ra == null || sra == null) {
return;
}
HttpServletRequest request = sra.getRequest();
ActionLog logContent = new ActionLog();
logContent.setTransactionId(request.getHeader("id"));
logContent.setRequest(JsonUtils.objectToJson(joinPoint.getArgs()));
long costTime = System.currentTimeMillis() - begin;
logContent.setCostTime(String.valueOf(costTime));
Log.log(logContent);
}
@After("controllerAspect()")
public void after(JoinPoint joinPoint) {
}
}