Skip to content

Commit

Permalink
Redirect sofa runtime log to console in test env (#1031)
Browse files Browse the repository at this point in the history
  • Loading branch information
crazysaltfish authored Oct 26, 2022
1 parent 186ba07 commit 620f9e0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/
public class LocalEnvUtil {
private static boolean LOCAL_ENV;
private static boolean TEST_ENV;

static {
// Currently supports for detection of IDEA IntelliJ
Expand All @@ -31,9 +32,23 @@ public class LocalEnvUtil {
} catch (ClassNotFoundException e) {
LOCAL_ENV = false;
}
// Detection of test environment
StackTraceElement[] stackTrace = new RuntimeException().getStackTrace();
for (StackTraceElement stackTraceElement : stackTrace) {
if ("loadContext".equals(stackTraceElement.getMethodName())
&& "org.springframework.boot.test.context.SpringBootContextLoader"
.equals(stackTraceElement.getClassName())) {
TEST_ENV = true;
break;
}
}
}

public static boolean isLocalEnv() {
return LOCAL_ENV;
}

public static boolean isTestEnv() {
return TEST_ENV;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private void prepare(ConfigurableEnvironment environment) {
}

private void defaultConsoleLoggers() {
if (LocalEnvUtil.isLocalEnv()) {
if (LocalEnvUtil.isLocalEnv() || LocalEnvUtil.isTestEnv()) {
CommonLoggingConfigurations.loadExternalConfiguration(
Constants.SOFA_MIDDLEWARE_ALL_LOG_CONSOLE_SWITCH, "true");
}
Expand Down

0 comments on commit 620f9e0

Please sign in to comment.