Browse Source

优化日志输出

24282 2 years ago
parent
commit
e45a2aaa71

+ 17 - 1
bladex-tool/blade-starter-log/src/main/java/org/springblade/core/log/aspect/RequestLogAspect.java

@@ -29,10 +29,14 @@ import org.springblade.core.tool.utils.ClassUtil;
 import org.springblade.core.tool.utils.StringPool;
 import org.springblade.core.tool.utils.StringUtil;
 import org.springblade.core.tool.utils.WebUtil;
+import org.springframework.beans.BeansException;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.ApplicationContextAware;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.core.MethodParameter;
+import org.springframework.core.env.Environment;
 import org.springframework.core.io.InputStreamSource;
 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RequestBody;
@@ -59,9 +63,10 @@ import java.util.concurrent.atomic.AtomicBoolean;
 @AllArgsConstructor
 @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
 @ConditionalOnProperty(value = BladeLogLevel.REQ_LOG_PROPS_PREFIX + ".enabled", havingValue = "true", matchIfMissing = true)
-public class RequestLogAspect {
+public class RequestLogAspect implements ApplicationContextAware {
 
 	private final BladeRequestLogProperties properties;
+	private static boolean enableLog = false;
 
 	/**
 	 * AOP 环切 控制器 R 返回值
@@ -76,6 +81,10 @@ public class RequestLogAspect {
 			"@within(org.springframework.web.bind.annotation.RestController))"
 	)
 	public Object aroundApi(ProceedingJoinPoint point) throws Throwable {
+		if (!enableLog) {
+			return point.proceed();
+		}
+
 		BladeLogLevel level = properties.getLevel();
 		// 不打印日志,直接返回
 		if (BladeLogLevel.NONE == level) {
@@ -257,4 +266,11 @@ public class RequestLogAspect {
 		}
 	}
 
+	@Override
+	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
+		Environment environment = applicationContext.getEnvironment();
+		String[] activeProfiles = environment.getActiveProfiles();
+		List<String> activeProfileList = Arrays.asList(activeProfiles);
+		enableLog = activeProfileList.contains("dev");
+	}
 }