Skip to content

Commit 072f10b

Browse files
QilongZhangstraybirdzls
authored andcommitted
Fix45 (rabbitmq#47)
1 parent ad8a164 commit 072f10b

File tree

6 files changed

+212
-52
lines changed

6 files changed

+212
-52
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,12 @@
3131
import org.springframework.core.env.EnumerablePropertySource;
3232
import org.springframework.core.env.PropertySource;
3333

34-
import java.util.*;
35-
import java.util.concurrent.atomic.AtomicBoolean;
34+
import java.util.Arrays;
35+
import java.util.HashMap;
36+
import java.util.HashSet;
37+
import java.util.Iterator;
38+
import java.util.Map;
39+
import java.util.Set;
3640

3741
import static com.alipay.sofa.common.log.Constants.*;
3842

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,14 @@
4040
import java.net.URL;
4141
import java.util.Map;
4242
import java.util.Properties;
43-
44-
import static com.alipay.sofa.common.log.Constants.*;
43+
import java.util.concurrent.atomic.AtomicLong;
44+
45+
import static com.alipay.sofa.common.log.Constants.SOFA_MIDDLEWARE_ALL_LOG_CONSOLE_LEVEL;
46+
import static com.alipay.sofa.common.log.Constants.SOFA_MIDDLEWARE_ALL_LOG_CONSOLE_SWITCH;
47+
import static com.alipay.sofa.common.log.Constants.SOFA_MIDDLEWARE_LOG_CONSOLE_LOG4J2_PATTERN;
48+
import static com.alipay.sofa.common.log.Constants.SOFA_MIDDLEWARE_LOG_CONSOLE_LOG4J2_PATTERN_DEFAULT;
49+
import static com.alipay.sofa.common.log.Constants.SOFA_MIDDLEWARE_SINGLE_LOG_CONSOLE_LEVEL;
50+
import static com.alipay.sofa.common.log.Constants.SOFA_MIDDLEWARE_SINGLE_LOG_CONSOLE_SWITCH;
4551

4652
/**
4753
* @author qilong.zql

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package com.alipay.sofa.common.boot.logging.log4j2;
1818

19+
import org.apache.logging.log4j.Level;
1920
import org.apache.logging.log4j.core.LoggerContext;
2021
import org.apache.logging.log4j.core.config.*;
2122
import org.apache.logging.log4j.core.config.plugins.Plugin;
@@ -51,6 +52,10 @@ public Configuration getConfiguration(LoggerContext loggerContext, Configuration
5152
public static final class SOFAConfiguration extends DefaultConfiguration {
5253
private SOFAConfiguration() {
5354
this.isShutdownHookEnabled = false;
55+
String levelName = System.getProperty(DefaultConfiguration.DEFAULT_LEVEL,
56+
Level.INFO.name());
57+
Level level = Level.valueOf(levelName);
58+
getRootLogger().setLevel(level != null ? level : Level.INFO);
5459
}
5560
}
5661

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.alipay.sofa.common.boot.logging.test;
18+
19+
import com.alipay.sofa.common.boot.logging.CommonLoggingApplicationListener;
20+
import com.alipay.sofa.common.log.LoggerSpaceManager;
21+
import com.alipay.sofa.common.log.MultiAppLoggerSpaceManager;
22+
import com.alipay.sofa.common.log.SpaceId;
23+
import org.junit.After;
24+
import org.junit.Before;
25+
import org.slf4j.Logger;
26+
27+
import java.io.ByteArrayOutputStream;
28+
import java.io.IOException;
29+
import java.io.PrintStream;
30+
import java.lang.reflect.Field;
31+
import java.util.Map;
32+
33+
/**
34+
* @author qilong.zql
35+
* @since 1.0.19
36+
*/
37+
public abstract class BaseLogIntegrationTest {
38+
public static final String TEST_SPACE = "test.space";
39+
40+
protected static Map<Object, Object> SPACES_MAP;
41+
protected static Logger logger;
42+
protected ByteArrayOutputStream outContent;
43+
protected ByteArrayOutputStream errContent;
44+
protected final PrintStream originalOut = System.out;
45+
protected final PrintStream originalErr = System.err;
46+
47+
static {
48+
try {
49+
Field spacesMapField = MultiAppLoggerSpaceManager.class.getDeclaredField("SPACES_MAP");
50+
spacesMapField.setAccessible(true);
51+
SPACES_MAP = (Map<Object, Object>) spacesMapField.get(MultiAppLoggerSpaceManager.class);
52+
} catch (Throwable throwable) {
53+
// ignore
54+
}
55+
}
56+
57+
@Before
58+
public void setUpStreams() {
59+
logger = LoggerSpaceManager.getLoggerBySpace(
60+
LogbackIntegrationTest.class.getCanonicalName(), TEST_SPACE);
61+
outContent = new ByteArrayOutputStream();
62+
errContent = new ByteArrayOutputStream();
63+
System.setOut(new PrintStream(outContent));
64+
System.setErr(new PrintStream(errContent));
65+
}
66+
67+
@After
68+
public void restoreStreams() throws IOException {
69+
System.setOut(originalOut);
70+
System.setErr(originalErr);
71+
outContent.close();
72+
errContent.close();
73+
SPACES_MAP.remove(new SpaceId(TEST_SPACE));
74+
new CommonLoggingApplicationListener().setReInitialize(false);
75+
}
76+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.alipay.sofa.common.boot.logging.test;
18+
19+
import com.alipay.sofa.common.log.Constants;
20+
import com.alipay.sofa.common.log.LoggerSpaceManager;
21+
import com.alipay.sofa.common.log.SpaceId;
22+
import com.alipay.sofa.common.log.env.LogEnvUtils;
23+
import org.apache.logging.log4j.core.appender.AbstractManager;
24+
import org.apache.logging.log4j.core.config.DefaultConfiguration;
25+
import org.junit.After;
26+
import org.junit.Assert;
27+
import org.junit.Before;
28+
import org.junit.Test;
29+
import org.springframework.boot.SpringApplication;
30+
31+
import java.io.IOException;
32+
import java.lang.reflect.Field;
33+
import java.util.HashMap;
34+
import java.util.Map;
35+
36+
/**
37+
* @author qilong.zql
38+
* @since 1.0.19
39+
*/
40+
public class Log4j2IntegrationTest extends BaseLogIntegrationTest {
41+
42+
protected static Map<Object, Object> MANAGER_MAP;
43+
44+
static {
45+
try {
46+
Field MAP = AbstractManager.class.getDeclaredField("MAP");
47+
MAP.setAccessible(true);
48+
MANAGER_MAP = (Map<Object, Object>) MAP.get(AbstractManager.class);
49+
} catch (Throwable throwable) {
50+
// ignore
51+
}
52+
}
53+
54+
@Before
55+
@Override
56+
public void setUpStreams() {
57+
MANAGER_MAP.clear();
58+
System.setProperty(Constants.LOGBACK_MIDDLEWARE_LOG_DISABLE_PROP_KEY, "true");
59+
super.setUpStreams();
60+
}
61+
62+
@After
63+
@Override
64+
public void restoreStreams() throws IOException {
65+
super.restoreStreams();
66+
System.getProperties().remove(Constants.LOGBACK_MIDDLEWARE_LOG_DISABLE_PROP_KEY);
67+
}
68+
69+
/**
70+
* test log4j2 info log to console
71+
*/
72+
@Test
73+
public void testLog4j2InfoToConsole() {
74+
Map<String, Object> properties = new HashMap<String, Object>();
75+
properties.put(
76+
String.format(Constants.SOFA_MIDDLEWARE_SINGLE_LOG_CONSOLE_SWITCH, TEST_SPACE), "true");
77+
SpringApplication springApplication = new SpringApplication(
78+
LogbackIntegrationTest.EmptyConfig.class);
79+
springApplication.setDefaultProperties(properties);
80+
springApplication.run(new String[] {});
81+
logger.info("space console");
82+
Assert.assertTrue(outContent.toString().contains("space console"));
83+
LogEnvUtils.processGlobalSystemLogProperties().remove(
84+
String.format(Constants.SOFA_MIDDLEWARE_SINGLE_LOG_CONSOLE_SWITCH, TEST_SPACE));
85+
}
86+
87+
/**
88+
* test log4j2 root level config
89+
*/
90+
@Test
91+
public void testRootLevelConfig() {
92+
SPACES_MAP.remove(new SpaceId(TEST_SPACE));
93+
System.setProperty(DefaultConfiguration.DEFAULT_LEVEL, "ERROR");
94+
95+
logger = LoggerSpaceManager.getLoggerBySpace(
96+
LogbackIntegrationTest.class.getCanonicalName(), TEST_SPACE);
97+
98+
Map<String, Object> properties = new HashMap<String, Object>();
99+
properties.put(
100+
String.format(Constants.SOFA_MIDDLEWARE_SINGLE_LOG_CONSOLE_SWITCH, TEST_SPACE), "true");
101+
SpringApplication springApplication = new SpringApplication(
102+
LogbackIntegrationTest.EmptyConfig.class);
103+
springApplication.setDefaultProperties(properties);
104+
springApplication.run(new String[] {});
105+
106+
logger.info("space info console");
107+
logger.error("space error console");
108+
Assert.assertFalse(outContent.toString().contains("space info console"));
109+
Assert.assertTrue(outContent.toString().contains("space error console"));
110+
111+
System.getProperties().remove(DefaultConfiguration.DEFAULT_LEVEL);
112+
LogEnvUtils.processGlobalSystemLogProperties().remove(
113+
String.format(Constants.SOFA_MIDDLEWARE_SINGLE_LOG_CONSOLE_SWITCH, TEST_SPACE));
114+
}
115+
}
Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,22 @@
1616
*/
1717
package com.alipay.sofa.common.boot.logging.test;
1818

19-
import com.alipay.sofa.common.boot.logging.CommonLoggingApplicationListener;
2019
import com.alipay.sofa.common.log.*;
2120
import com.alipay.sofa.common.log.env.LogEnvUtils;
2221
import com.alipay.sofa.common.utils.ReportUtil;
2322
import com.alipay.sofa.common.utils.StringUtil;
2423
import org.apache.commons.io.FileUtils;
2524
import org.apache.logging.log4j.ThreadContext;
26-
import org.junit.After;
2725
import org.junit.Assert;
28-
import org.junit.Before;
2926
import org.junit.Test;
30-
import org.slf4j.Logger;
3127
import org.springframework.boot.SpringApplication;
3228
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
3329
import org.springframework.context.ConfigurableApplicationContext;
3430
import org.springframework.context.annotation.Configuration;
3531
import org.springframework.core.env.Environment;
3632

37-
import java.io.ByteArrayOutputStream;
3833
import java.io.File;
3934
import java.io.IOException;
40-
import java.io.PrintStream;
41-
import java.lang.reflect.Field;
4235
import java.util.HashMap;
4336
import java.util.List;
4437
import java.util.Map;
@@ -47,46 +40,7 @@
4740
* @author qilong.zql
4841
* @since 1.0.15
4942
*/
50-
public class LogIntegrationTest {
51-
52-
public static final String TEST_SPACE = "test.space";
53-
public static Logger logger;
54-
55-
private ByteArrayOutputStream outContent;
56-
private ByteArrayOutputStream errContent;
57-
private final PrintStream originalOut = System.out;
58-
private final PrintStream originalErr = System.err;
59-
private static Map<Object, Object> SPACES_MAP;
60-
61-
static {
62-
try {
63-
Field spacesMapField = MultiAppLoggerSpaceManager.class.getDeclaredField("SPACES_MAP");
64-
spacesMapField.setAccessible(true);
65-
SPACES_MAP = (Map<Object, Object>) spacesMapField.get(MultiAppLoggerSpaceManager.class);
66-
} catch (Throwable throwable) {
67-
// ignore
68-
}
69-
}
70-
71-
@Before
72-
public void setUpStreams() {
73-
logger = LoggerSpaceManager.getLoggerBySpace(LogIntegrationTest.class.getCanonicalName(),
74-
TEST_SPACE);
75-
outContent = new ByteArrayOutputStream();
76-
errContent = new ByteArrayOutputStream();
77-
System.setOut(new PrintStream(outContent));
78-
System.setErr(new PrintStream(errContent));
79-
}
80-
81-
@After
82-
public void restoreStreams() throws IOException {
83-
System.setOut(originalOut);
84-
System.setErr(originalErr);
85-
outContent.close();
86-
errContent.close();
87-
SPACES_MAP.remove(new SpaceId(TEST_SPACE));
88-
new CommonLoggingApplicationListener().setReInitialize(false);
89-
}
43+
public class LogbackIntegrationTest extends BaseLogIntegrationTest {
9044

9145
@Test
9246
public void testDefaultLevel() throws IOException {
@@ -340,7 +294,7 @@ public void testThreadContextConfiguration() {
340294
try {
341295
System.setProperty(Constants.LOGBACK_MIDDLEWARE_LOG_DISABLE_PROP_KEY, "true");
342296
SPACES_MAP.remove(new SpaceId(TEST_SPACE));
343-
LoggerSpaceManager.getLoggerBySpace(LogIntegrationTest.class.getCanonicalName(),
297+
LoggerSpaceManager.getLoggerBySpace(LogbackIntegrationTest.class.getCanonicalName(),
344298
TEST_SPACE);
345299
ThreadContext.put("testKey", "testValue");
346300
ThreadContext.put("logging.path", "anyPath");

0 commit comments

Comments
 (0)