|
@@ -1,14 +1,16 @@
|
|
package com.chelvc.framework.base.interceptor;
|
|
package com.chelvc.framework.base.interceptor;
|
|
|
|
|
|
-import com.chelvc.framework.base.context.ApplicationContextHolder;
|
|
|
|
|
|
+import ch.qos.logback.classic.LoggerContext;
|
|
import com.chelvc.framework.base.context.LoggingContextHolder;
|
|
import com.chelvc.framework.base.context.LoggingContextHolder;
|
|
import com.chelvc.framework.base.context.SessionContextHolder;
|
|
import com.chelvc.framework.base.context.SessionContextHolder;
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
import org.aspectj.lang.JoinPoint;
|
|
import org.aspectj.lang.JoinPoint;
|
|
import org.aspectj.lang.ProceedingJoinPoint;
|
|
import org.aspectj.lang.ProceedingJoinPoint;
|
|
import org.aspectj.lang.Signature;
|
|
import org.aspectj.lang.Signature;
|
|
import org.aspectj.lang.annotation.Around;
|
|
import org.aspectj.lang.annotation.Around;
|
|
import org.aspectj.lang.annotation.Aspect;
|
|
import org.aspectj.lang.annotation.Aspect;
|
|
import org.aspectj.lang.reflect.MethodSignature;
|
|
import org.aspectj.lang.reflect.MethodSignature;
|
|
|
|
+import org.slf4j.ILoggerFactory;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.boot.web.servlet.error.ErrorController;
|
|
import org.springframework.boot.web.servlet.error.ErrorController;
|
|
@@ -23,25 +25,34 @@ import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
* @author Woody
|
|
* @author Woody
|
|
* @date 2024/1/30
|
|
* @date 2024/1/30
|
|
*/
|
|
*/
|
|
|
|
+@Slf4j
|
|
@Aspect
|
|
@Aspect
|
|
@Component
|
|
@Component
|
|
@Order(Ordered.HIGHEST_PRECEDENCE)
|
|
@Order(Ordered.HIGHEST_PRECEDENCE)
|
|
public class ControllerAccessInterceptor {
|
|
public class ControllerAccessInterceptor {
|
|
/**
|
|
/**
|
|
- * 判断是否开启调试日志
|
|
|
|
|
|
+ * 获取接口方法Logger实例
|
|
*
|
|
*
|
|
* @param point 接口方法拦截点
|
|
* @param point 接口方法拦截点
|
|
- * @return true/false
|
|
|
|
|
|
+ * @return Logger实例
|
|
*/
|
|
*/
|
|
- private boolean isDebug(JoinPoint point) {
|
|
|
|
- Object target = point.getTarget();
|
|
|
|
- if (target instanceof ErrorController
|
|
|
|
- || !ApplicationContextHolder.getProperty("controller.debug.enabled", boolean.class, false)) {
|
|
|
|
- return false;
|
|
|
|
|
|
+ private Logger getLogger(JoinPoint point) {
|
|
|
|
+ // 排除ErrorController接口
|
|
|
|
+ if (point.getTarget() instanceof ErrorController) {
|
|
|
|
+ return null;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ // 排除@ExceptionHandler注解方法
|
|
Signature signature = point.getSignature();
|
|
Signature signature = point.getSignature();
|
|
- return signature instanceof MethodSignature
|
|
|
|
- && !((MethodSignature) signature).getMethod().isAnnotationPresent(ExceptionHandler.class);
|
|
|
|
|
|
+ if (signature instanceof MethodSignature
|
|
|
|
+ && ((MethodSignature) signature).getMethod().isAnnotationPresent(ExceptionHandler.class)) {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 获取Logger实例
|
|
|
|
+ ILoggerFactory factory = LoggerFactory.getILoggerFactory();
|
|
|
|
+ String name = signature.getDeclaringTypeName() + "." + signature.getName();
|
|
|
|
+ return factory instanceof LoggerContext ? ((LoggerContext) factory).exists(name) : factory.getLogger(name);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -54,9 +65,13 @@ public class ControllerAccessInterceptor {
|
|
"|| @within(org.springframework.web.bind.annotation.RestController)")
|
|
"|| @within(org.springframework.web.bind.annotation.RestController)")
|
|
public Object logging(ProceedingJoinPoint point) throws Throwable {
|
|
public Object logging(ProceedingJoinPoint point) throws Throwable {
|
|
Object value = point.proceed();
|
|
Object value = point.proceed();
|
|
- if (this.isDebug(point)) {
|
|
|
|
- Logger logger = LoggerFactory.getLogger(point.getSignature().getDeclaringTypeName());
|
|
|
|
- LoggingContextHolder.debug(logger, SessionContextHolder.getRequest(), value);
|
|
|
|
|
|
+ try {
|
|
|
|
+ Logger logger = this.getLogger(point);
|
|
|
|
+ if (logger != null) {
|
|
|
|
+ LoggingContextHolder.debug(logger, SessionContextHolder.getRequest(), value);
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error("Controller access logging failed", e);
|
|
}
|
|
}
|
|
return value;
|
|
return value;
|
|
}
|
|
}
|