@Component
//切面
@Aspect
public class LoggerConfig {
private Logger logger = LoggerFactory.getLogger(LoggerConfig.class);
//切入点
// @Pointcut("execution(* com.qfedu.service.impl..*.*(..))")
@Pointcut("@annotation(com.acedu.aop.SysLogger)")
public void pt1() {
}//签名
@Around("pt1()")
public Object arround(ProceedingJoinPoint proceedingJoinPoint) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
//请求的内容,记录url、method、ip
System.out.println("URL==>" + request.getRequestURL().toString() + "," +
"METHOD==>" + request.getMethod() + "," +
"IP==>" + request.getRemoteAddr());
//1.记录请求的接口,以及时间
MethodSignature signature = (MethodSignature) proceedingJoinPoint.getSignature();
Method method = signature.getMethod();
String[] parameterNames = signature.getParameterNames();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat retrunSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//1.logger日志 5个级别 info debug,error,waring,trace
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("===>进入了方法:" + method.getName());
stringBuffer.append("===>参数为:{}" + Arrays.toString(parameterNames));
stringBuffer.append("===>当前时间为" + simpleDateFormat.format(new Date()));
// logger.info(stringBuffer.toString());
logger.debug(stringBuffer.toString());
Object proceed = null;
try {
proceed = proceedingJoinPoint.proceed();
logger.debug("方法执行完成,时间为:{}" + retrunSimpleDateFormat.format(new Date()));
return proceed;
} catch (Throwable throwable) {
throwable.printStackTrace();
logger.error("当前接口出现异常:" + throwable.getMessage());
}
return proceed;
}
}