|
|
@@ -0,0 +1,57 @@
|
|
|
+package nju.seec.helper.aop.log;
|
|
|
+
|
|
|
+import lombok.SneakyThrows;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.aspectj.lang.JoinPoint;
|
|
|
+import org.aspectj.lang.ProceedingJoinPoint;
|
|
|
+import org.aspectj.lang.annotation.AfterThrowing;
|
|
|
+import org.aspectj.lang.annotation.Around;
|
|
|
+import org.aspectj.lang.annotation.Aspect;
|
|
|
+import org.aspectj.lang.annotation.Pointcut;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.web.context.request.RequestContextHolder;
|
|
|
+import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author cst
|
|
|
+ */
|
|
|
+@Component
|
|
|
+@Slf4j
|
|
|
+@Aspect
|
|
|
+public class LogAspect {
|
|
|
+ @Pointcut("execution(public * nju.seec.helper.web.controller..*.*(..))")
|
|
|
+ public void controllerPointcut() {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @SneakyThrows
|
|
|
+ @Around("controllerPointcut()")
|
|
|
+ public Object aroundController(ProceedingJoinPoint joinPoint) {
|
|
|
+ ServletRequestAttributes requestAttributes = (ServletRequestAttributes)
|
|
|
+ RequestContextHolder.getRequestAttributes();
|
|
|
+
|
|
|
+ if (requestAttributes != null) {
|
|
|
+ HttpServletRequest request = requestAttributes.getRequest();
|
|
|
+ log.info("IP地址 " + request.getRemoteAddr() + " 访问 " +
|
|
|
+ joinPoint.getTarget().getClass().getName() + " 类 " +
|
|
|
+ joinPoint.getSignature().getName() + " 方法");
|
|
|
+ }
|
|
|
+
|
|
|
+ return joinPoint.proceed();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Pointcut("execution(public * nju.seec.helper.logic.service.impl..*.*(..))")
|
|
|
+ public void servicePointcut() {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @AfterThrowing(pointcut = "servicePointcut()", throwing = "e")
|
|
|
+ public void afterServiceThrowing(JoinPoint joinPoint, Throwable e) {
|
|
|
+ log.error(joinPoint.getTarget().getClass().getName() + " 类 " +
|
|
|
+ joinPoint.getSignature().getName() + " 方法出现异常:" +
|
|
|
+ e.getLocalizedMessage()
|
|
|
+ );
|
|
|
+ }
|
|
|
+}
|