Skip to content

Commit 38d517f

Browse files
authored
Fix string Index out of range bug when parse property key. (#43)
1 parent 3f15dfe commit 38d517f

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

core/src/main/java/com/alipay/sofa/common/log/Constants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ public interface Constants {
7777
String SOFA_MIDDLEWARE_ALL_LOG_CONSOLE_SWITCH = "sofa.middleware.log.console";
7878
// single space switch to log on console
7979
String SOFA_MIDDLEWARE_SINGLE_LOG_CONSOLE_SWITCH = "sofa.middleware.log.%s.console";
80+
// console string
81+
String CONSOLE_SUFFIX = ".console";
8082
// sofa-common-tools 自身日志开关
8183
// internal log level config.
8284
String SOFA_MIDDLEWARE_LOG_INTERNAL_LEVEL = "sofa.middleware.log.internal.level";

log-sofa-boot-starter/src/main/java/com/alipay/sofa/common/boot/logging/CommonLoggingApplicationListener.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,11 @@ public void setReInitialize(boolean value) {
5959
public void reInitializeLog(Map<String, String> context) {
6060
for (String key : context.keySet()) {
6161
if (key.startsWith(Constants.SOFA_MIDDLEWARE_CONFIG_PREFIX)
62-
&& !key.equals(Constants.SOFA_MIDDLEWARE_ALL_LOG_CONSOLE_SWITCH)) {
62+
&& !key.equals(Constants.SOFA_MIDDLEWARE_ALL_LOG_CONSOLE_SWITCH)
63+
&& key.endsWith(Constants.CONSOLE_SUFFIX)) {
6364
int index = Constants.SOFA_MIDDLEWARE_CONFIG_PREFIX.length();
6465
// minus length of .console
65-
int end = key.length() - 8;
66+
int end = key.length() - Constants.CONSOLE_SUFFIX.length();
6667
String spaceId = key.substring(index, end);
6768
LoggerSpaceManager.getLoggerBySpace(spaceId, spaceId);
6869
}

log-sofa-boot-starter/src/test/java/com/alipay/sofa/common/boot/logging/test/LogIntegrationTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,24 @@ public void testThreadContextConfiguration() {
356356
}
357357
}
358358

359+
/**
360+
* test sofa.middleware.log.disable
361+
*/
362+
@Test
363+
public void testDisableMiddleLog() {
364+
Map<String, Object> properties = new HashMap<String, Object>();
365+
properties.put(Constants.SOFA_MIDDLEWARE_LOG_DISABLE_PROP_KEY, "true");
366+
SpringApplication springApplication = new SpringApplication(EmptyConfig.class);
367+
springApplication.setDefaultProperties(properties);
368+
springApplication.run(new String[] {});
369+
logger.info("global space console");
370+
logger.debug("global space console debug");
371+
Assert.assertFalse(outContent.toString().contains("global space console"));
372+
Assert.assertFalse(outContent.toString().contains("global space console debug"));
373+
LogEnvUtils.processGlobalSystemLogProperties().remove(
374+
Constants.SOFA_MIDDLEWARE_LOG_DISABLE_PROP_KEY);
375+
}
376+
359377
protected File getLogbackDefaultFile(Environment environment) {
360378
String loggingRoot = environment.getProperty(Constants.LOG_PATH_PREFIX + TEST_SPACE);
361379
if (StringUtil.isBlank(loggingRoot)) {

0 commit comments

Comments
 (0)