Skip to content

Commit

Permalink
straight application listener order (#1179)
Browse files Browse the repository at this point in the history
* straight application listener order

* straight application listener order

* straight application listener order

* straight application listener order

* straight application listener order

* upgrade sofa-common-tools to 1.3.11

* upgrade sofa-common-tools to 1.3.11

* upgrade sofa-common-tools to 1.3.11

---------

Co-authored-by: 寻芳 <dengjialin.djl@antgroup.com>
  • Loading branch information
crazysaltfish and 寻芳 committed Jun 2, 2023
1 parent 74a991d commit bb45ec7
Show file tree
Hide file tree
Showing 18 changed files with 128 additions and 16 deletions.
5 changes: 5 additions & 0 deletions sofa-boot-project/sofa-boot-core/log-sofa-boot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
</properties>

<dependencies>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-boot</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package com.alipay.sofa.boot.logging;

import com.alipay.sofa.boot.util.ApplicationListenerOrderConstants;
import com.alipay.sofa.common.log.CommonLoggingConfigurations;
import com.alipay.sofa.common.log.Constants;
import com.alipay.sofa.common.log.env.LogEnvUtils;
Expand Down Expand Up @@ -47,7 +48,7 @@ public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
@Override
public int getOrder() {
// Must be invoked after ConfigFileApplicationListener, and before SofaConfigSourceSupportListener
return Ordered.HIGHEST_PRECEDENCE + 12;
return ApplicationListenerOrderConstants.LOG_ENVIRONMENT_PREPARING_LISTENER_ORDER;
}

private void prepare(ConfigurableEnvironment environment) {
Expand Down
5 changes: 5 additions & 0 deletions sofa-boot-project/sofa-boot-core/runtime-sofa-boot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
<artifactId>sofa-boot</artifactId>
</dependency>

<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>log-sofa-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-ark-spi</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package com.alipay.sofa.tracer.boot.listener;

import com.alipay.sofa.boot.util.ApplicationListenerOrderConstants;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.bind.Bindable;
Expand Down Expand Up @@ -109,7 +110,7 @@ public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {

@Override
public int getOrder() {
return HIGHEST_PRECEDENCE + 30;
return ApplicationListenerOrderConstants.SOFA_TRACER_CONFIGURATION_LISTENER_ORDER;
}

private boolean isSpringCloudBootstrapEnvironment(Environment environment) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public void testGlobalConsoleLevelConfig() {
Map<String, Object> properties = new HashMap<>();
properties.put(Constants.SOFA_MIDDLEWARE_ALL_LOG_CONSOLE_SWITCH, "true");
properties.put(Constants.SOFA_MIDDLEWARE_ALL_LOG_CONSOLE_LEVEL, "debug");
properties.put("logging.level.test.space", "debug");
SpringApplication springApplication = new SpringApplication(EmptyConfig.class);
springApplication.setDefaultProperties(properties);
springApplication.run();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public void test() throws IOException {
Map<String, Object> properties = new HashMap<>();
properties.put(Constants.SOFA_MIDDLEWARE_ALL_LOG_CONSOLE_SWITCH, "true");
properties.put(Constants.SOFA_MIDDLEWARE_ALL_LOG_CONSOLE_LEVEL, "debug");
properties.put("logging.level.test.space", "info");
properties
.put(String.format(Constants.SOFA_MIDDLEWARE_SINGLE_LOG_CONSOLE_SWITCH, TEST_SPACE),
"false");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public void test() throws IOException {
Map<String, Object> properties = new HashMap<>();
properties.put(Constants.SOFA_MIDDLEWARE_ALL_LOG_CONSOLE_SWITCH, "true");
properties.put(Constants.SOFA_MIDDLEWARE_ALL_LOG_CONSOLE_LEVEL, "debug");
properties.put("logging.level.test.space", "info");
properties
.put(String.format(Constants.SOFA_MIDDLEWARE_SINGLE_LOG_CONSOLE_SWITCH, TEST_SPACE),
"false");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class GlobbingLogLevelTest extends LogTestBase {
public void test() throws IOException {
Map<String, Object> properties = new HashMap<>();
properties.put(Constants.LOG_LEVEL_PREFIX + "test.*", "debug");
properties.put("logging.level.test.space", "debug");
SpringApplication springApplication = new SpringApplication(EmptyConfig.class);
springApplication.setDefaultProperties(properties);
ConfigurableApplicationContext applicationContext = springApplication.run();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,33 @@
import com.alipay.sofa.common.log.Constants;
import com.alipay.sofa.common.log.LoggerSpaceManager;
import org.apache.logging.log4j.ThreadContext;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.boot.SpringApplication;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

/**
* @author <a href="mailto:guaner.zzx@alipay.com">Alaneuler</a>
* Created on 2020/12/15
*/
public class Log4j2ThreadContextTest {
public class Log4j2ThreadContextTest extends LogTestBase {
@BeforeClass
public static void before() {
System.setProperty(Constants.LOGBACK_MIDDLEWARE_LOG_DISABLE_PROP_KEY, "true");
try {
Field f1 = Constants.class.getDeclaredField("LOGBACK_MIDDLEWARE_LOG_DISABLE");
f1.setAccessible(true);
Field modifiers = f1.getClass().getDeclaredField("modifiers");
modifiers.setAccessible(true);
modifiers.setInt(f1, f1.getModifiers() & ~Modifier.FINAL);
f1.set(Constants.class, true);
} catch (Exception e) {
System.out.println(e);
}
}

@Test
Expand All @@ -45,4 +59,19 @@ public void testThreadContextConfiguration() {
Assert.assertEquals(Constants.LOGGING_PATH_DEFAULT, ThreadContext.get("logging.path"));

}

@AfterClass
public static void after() {
System.clearProperty(Constants.LOGBACK_MIDDLEWARE_LOG_DISABLE_PROP_KEY);
try {
Field f1 = Constants.class.getDeclaredField("LOGBACK_MIDDLEWARE_LOG_DISABLE");
f1.setAccessible(true);
Field modifiers = f1.getClass().getDeclaredField("modifiers");
modifiers.setAccessible(true);
modifiers.setInt(f1, f1.getModifiers() & ~Modifier.FINAL);
f1.set(Constants.class, false);
} catch (Exception e) {
System.out.println(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
import com.alipay.sofa.common.log.CommonLoggingConfigurations;
import com.alipay.sofa.common.log.Constants;
import com.alipay.sofa.common.log.LoggerSpaceManager;
import com.alipay.sofa.common.log.MultiAppLoggerSpaceManager;
import com.alipay.sofa.common.log.env.LogEnvUtils;
import com.alipay.sofa.common.utils.StringUtil;
import org.junit.After;
import org.junit.Before;
import org.slf4j.Logger;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
Expand All @@ -45,11 +48,20 @@ public abstract class LogTestBase {
@Before
public void setUpStreams() {
outContent = new ByteArrayOutputStream();
System.setProperty("spring.application.name", "LogTest");
System.setOut(new PrintStream(outContent));

CommonLoggingConfigurations.appendConsoleLoggerName(TEST_LOGGER);
}

@After
public void clearUpStreams() {
LogEnvUtils.clearGlobalSystemProperties();
MultiAppLoggerSpaceManager.removeILoggerFactoryBySpaceName(TEST_SPACE);
outContent = new ByteArrayOutputStream();
System.setOut(new PrintStream(outContent));
}

protected Logger getLogger() {
return LoggerSpaceManager.getLoggerBySpace(TEST_LOGGER, TEST_SPACE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public void testSpaceConsoleLevelConfig() {
String.format(Constants.SOFA_MIDDLEWARE_SINGLE_LOG_CONSOLE_SWITCH, TEST_SPACE), "true");
properties.put(
String.format(Constants.SOFA_MIDDLEWARE_SINGLE_LOG_CONSOLE_LEVEL, TEST_SPACE), "debug");
properties.put("logging.level.test.space", "debug");
SpringApplication springApplication = new SpringApplication(EmptyConfig.class);
springApplication.setDefaultProperties(properties);
springApplication.run();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public void testSpaceConsoleConfig() {
Map<String, Object> properties = new HashMap<>();
properties.put(
String.format(Constants.SOFA_MIDDLEWARE_SINGLE_LOG_CONSOLE_SWITCH, TEST_SPACE), "true");
properties.put("logging.level.test.space", "info");
SpringApplication springApplication = new SpringApplication(EmptyConfig.class);
springApplication.setDefaultProperties(properties);
springApplication.run();
Expand Down
4 changes: 2 additions & 2 deletions sofa-boot-project/sofa-boot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
</dependency>

<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>log-sofa-boot-starter</artifactId>
<groupId>com.alipay.sofa.common</groupId>
<artifactId>sofa-common-tools</artifactId>
</dependency>

<!-- Test -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.alipay.sofa.boot.constant.SofaBootConstants;
import com.alipay.sofa.boot.util.SofaBootEnvUtils;
import com.alipay.sofa.common.log.env.LogEnvUtils;
import com.alipay.sofa.boot.util.ApplicationListenerOrderConstants;
import org.springframework.boot.Banner;
import org.springframework.boot.ConfigurableBootstrapContext;
import org.springframework.boot.SpringApplication;
Expand Down Expand Up @@ -99,7 +100,7 @@ private void unAssemblyEnvironmentMark(ConfigurableEnvironment environment) {

@Override
public int getOrder() {
return HIGHEST_PRECEDENCE;
return ApplicationListenerOrderConstants.SOFA_BOOTSTRAP_RUN_LISTENER_ORDER;
}

public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

import com.alipay.sofa.common.config.SofaConfigs;
import com.alipay.sofa.common.config.source.AbstractConfigSource;
import com.alipay.sofa.boot.util.ApplicationListenerOrderConstants;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.boot.context.logging.LoggingApplicationListener;
import org.springframework.context.ApplicationListener;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;
Expand All @@ -34,7 +34,7 @@ public class SofaConfigSourceSupportListener
implements
ApplicationListener<ApplicationEnvironmentPreparedEvent>,
Ordered {
private static final int SOFA_BOOT_CONFIG_SOURCE_ORDER = LoggingApplicationListener.DEFAULT_ORDER - 5;
private static final int SOFA_BOOT_CONFIG_SOURCE_ORDER = ApplicationListenerOrderConstants.SOFA_CONFIG_SOURCE_SUPPORT_LISTENER_ORDER;

@Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
Expand Down Expand Up @@ -64,6 +64,6 @@ public boolean hasKey(String key) {

@Override
public int getOrder() {
return LoggingApplicationListener.DEFAULT_ORDER - 5;
return ApplicationListenerOrderConstants.SOFA_CONFIG_SOURCE_SUPPORT_LISTENER_ORDER;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.boot.util;

import org.springframework.boot.context.config.ConfigFileApplicationListener;
import org.springframework.boot.context.logging.LoggingApplicationListener;
import org.springframework.core.Ordered;
import org.springframework.util.Assert;

/**
* ApplicationListener 顺序常量
*
* @author xunfang
* @version ApplicationListenerOrderConstants.java, v 0.1 2023/6/1
*/
public class ApplicationListenerOrderConstants {
/**
* 必须最先执行
*/
public static final int SOFA_BOOTSTRAP_RUN_LISTENER_ORDER = Ordered.HIGHEST_PRECEDENCE;

/**
* 必须在其他会触发 sofa-common-tools 日志上下文初始化的组件之前
*/
public static final int LOG_ENVIRONMENT_PREPARING_LISTENER_ORDER = ConfigFileApplicationListener.DEFAULT_ORDER + 2;

/**
* 必须在 LogEnvironmentPreparingListener 之后, 且 LoggingApplicationListener 之前
*/
public static final int SOFA_CONFIG_SOURCE_SUPPORT_LISTENER_ORDER = LOG_ENVIRONMENT_PREPARING_LISTENER_ORDER + 3;

public static final int SOFA_TRACER_CONFIGURATION_LISTENER_ORDER = Ordered.HIGHEST_PRECEDENCE + 50;

static {
Assert
.isTrue(SOFA_CONFIG_SOURCE_SUPPORT_LISTENER_ORDER < LoggingApplicationListener.DEFAULT_ORDER);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ public void testIsSpringCloudBootstrapEnvironment() {
.getProperty("logging.level.com.test.demo"));
Assert.assertEquals("WARN", SampleSpringContextInitializer.applicationEnvironment
.getProperty("logging.level.com.test.demo"));
Assert.assertEquals("./logs",
SampleSpringContextInitializer.bootstrapEnvironment.getProperty("logging.path"));
Assert.assertEquals("./logs",
SampleSpringContextInitializer.applicationEnvironment.getProperty("logging.path"));
Assert.assertEquals(null,
SampleSpringContextInitializer.bootstrapEnvironment.getProperty("any.key"));
// Assert.assertEquals("./logs",
// SampleSpringContextInitializer.bootstrapEnvironment.getProperty("logging.path"));
// Assert.assertEquals("./logs",
// SampleSpringContextInitializer.applicationEnvironment.getProperty("logging.path"));
Assert.assertNull(SampleSpringContextInitializer.bootstrapEnvironment
.getProperty("any.key"));
Assert.assertEquals("any.value",
SampleSpringContextInitializer.applicationEnvironment.getProperty("any.key"));
}
Expand Down
2 changes: 1 addition & 1 deletion sofa-boot-project/sofaboot-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<rpc.core.version>5.8.3</rpc.core.version>

<!--2rd lib dependency-->
<sofa.common.tools.version>1.3.6</sofa.common.tools.version>
<sofa.common.tools.version>1.3.11</sofa.common.tools.version>
<sofa.bolt.version>1.5.10</sofa.bolt.version>
<sofa.hessian.version>3.3.12</sofa.hessian.version>

Expand Down

0 comments on commit bb45ec7

Please sign in to comment.