Skip to content

Commit cbbb90e

Browse files
HzjNeverStop“HzjNeverStop”
and
“HzjNeverStop”
authored
Fix init loggerSpace in concurrency (#193)
Co-authored-by: “HzjNeverStop” <“[email protected]”>
1 parent 7841cae commit cbbb90e

File tree

4 files changed

+89
-1
lines changed

4 files changed

+89
-1
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>com.alipay.sofa.common</groupId>
66
<artifactId>sofa-common-tools</artifactId>
7-
<version>2.0.2</version>
7+
<version>2.0.3</version>
88
<packaging>jar</packaging>
99

1010
<name>${project.groupId}:${project.artifactId}</name>

src/main/java/com/alipay/sofa/common/log/MultiAppLoggerSpaceManager.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ private static AbstractLoggerSpaceFactory getILoggerFactoryBySpaceName(SpaceId s
193193
if (!isSpaceILoggerFactoryExisted(spaceId)) {
194194
factory = createILoggerFactory(spaceId, space, spaceClassloader);
195195
space.setAbstractLoggerSpaceFactory(factory);
196+
} else {
197+
factory = LOG_FACTORY_MAP.get(spaceId).getAbstractLoggerSpaceFactory();
196198
}
197199
}
198200
} else {
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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.log;
18+
19+
import org.junit.Assert;
20+
import org.junit.Test;
21+
import org.slf4j.Logger;
22+
23+
import java.util.ArrayList;
24+
import java.util.List;
25+
import java.util.concurrent.CopyOnWriteArrayList;
26+
import java.util.concurrent.CountDownLatch;
27+
import java.util.concurrent.LinkedBlockingQueue;
28+
import java.util.concurrent.ThreadPoolExecutor;
29+
import java.util.concurrent.TimeUnit;
30+
31+
/**
32+
* @author huzijie
33+
* @version LoggerSpaceConcurrencyTest.java, v 0.1 2023年11月07日 3:57 PM huzijie Exp $
34+
*/
35+
public class LoggerSpaceConcurrencyTest {
36+
private static final ThreadPoolExecutor EXECUTOR = new ThreadPoolExecutor(
37+
5, 50, 60, TimeUnit.SECONDS,
38+
new LinkedBlockingQueue<>(1000));
39+
40+
41+
@Test
42+
public void testLogUtilGetLoggerConcurrently() throws InterruptedException {
43+
List<Logger> loggers = new CopyOnWriteArrayList<>();
44+
45+
CountDownLatch countDownLatch = new CountDownLatch(10);
46+
for (int i = 0; i < 10; i++) {
47+
EXECUTOR.submit(() -> {
48+
try {
49+
Logger logger = getLogger();
50+
loggers.add(logger);
51+
} catch (Throwable t) {
52+
t.printStackTrace();
53+
} finally {
54+
countDownLatch.countDown();
55+
}
56+
});
57+
}
58+
59+
Assert.assertTrue(countDownLatch.await(10, TimeUnit.SECONDS));
60+
Assert.assertEquals(loggers.size(), 10);
61+
62+
for (Logger logger : loggers) {
63+
Assert.assertNotEquals("Constants.DEFAULT_LOG is not expected", logger, Constants.DEFAULT_LOG);
64+
}
65+
66+
}
67+
68+
private Logger getLogger() {
69+
return LoggerSpaceManager.getLoggerBySpace("TestLogger", "com.alipay.sofa.concurrency");
70+
}
71+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<configuration>
3+
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
4+
<encoder charset="UTF-8">
5+
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
6+
</encoder>
7+
</appender>
8+
9+
<logger name="com.foo.Bar" level="${logging.level.com.alipay.sofa.rpc}" additivity="false">
10+
<appender-ref ref="stdout"/>
11+
</logger>
12+
<!-- <root level="DEBUG">
13+
<appender-ref ref="stdout" />
14+
</root>-->
15+
</configuration>

0 commit comments

Comments
 (0)