diff --git a/pom.xml b/pom.xml index 5addb85..2bdc337 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ com.alipay.sofa.common sofa-common-tools - 2.0.2 + 2.0.3 jar ${project.groupId}:${project.artifactId} diff --git a/src/main/java/com/alipay/sofa/common/log/MultiAppLoggerSpaceManager.java b/src/main/java/com/alipay/sofa/common/log/MultiAppLoggerSpaceManager.java index 1d39fb8..298940c 100644 --- a/src/main/java/com/alipay/sofa/common/log/MultiAppLoggerSpaceManager.java +++ b/src/main/java/com/alipay/sofa/common/log/MultiAppLoggerSpaceManager.java @@ -193,6 +193,8 @@ private static AbstractLoggerSpaceFactory getILoggerFactoryBySpaceName(SpaceId s if (!isSpaceILoggerFactoryExisted(spaceId)) { factory = createILoggerFactory(spaceId, space, spaceClassloader); space.setAbstractLoggerSpaceFactory(factory); + } else { + factory = LOG_FACTORY_MAP.get(spaceId).getAbstractLoggerSpaceFactory(); } } } else { diff --git a/src/test/java/com/alipay/sofa/common/log/LoggerSpaceConcurrencyTest.java b/src/test/java/com/alipay/sofa/common/log/LoggerSpaceConcurrencyTest.java new file mode 100644 index 0000000..2cc85f1 --- /dev/null +++ b/src/test/java/com/alipay/sofa/common/log/LoggerSpaceConcurrencyTest.java @@ -0,0 +1,71 @@ +/* + * 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.common.log; + +import org.junit.Assert; +import org.junit.Test; +import org.slf4j.Logger; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +/** + * @author huzijie + * @version LoggerSpaceConcurrencyTest.java, v 0.1 2023年11月07日 3:57 PM huzijie Exp $ + */ +public class LoggerSpaceConcurrencyTest { + private static final ThreadPoolExecutor EXECUTOR = new ThreadPoolExecutor( + 5, 50, 60, TimeUnit.SECONDS, + new LinkedBlockingQueue<>(1000)); + + + @Test + public void testLogUtilGetLoggerConcurrently() throws InterruptedException { + List loggers = new CopyOnWriteArrayList<>(); + + CountDownLatch countDownLatch = new CountDownLatch(10); + for (int i = 0; i < 10; i++) { + EXECUTOR.submit(() -> { + try { + Logger logger = getLogger(); + loggers.add(logger); + } catch (Throwable t) { + t.printStackTrace(); + } finally { + countDownLatch.countDown(); + } + }); + } + + Assert.assertTrue(countDownLatch.await(10, TimeUnit.SECONDS)); + Assert.assertEquals(loggers.size(), 10); + + for (Logger logger : loggers) { + Assert.assertNotEquals("Constants.DEFAULT_LOG is not expected", logger, Constants.DEFAULT_LOG); + } + + } + + private Logger getLogger() { + return LoggerSpaceManager.getLoggerBySpace("TestLogger", "com.alipay.sofa.concurrency"); + } +} diff --git a/src/test/resources/com/alipay/sofa/concurrency/log/logback/log-conf.xml b/src/test/resources/com/alipay/sofa/concurrency/log/logback/log-conf.xml new file mode 100644 index 0000000..d341ec1 --- /dev/null +++ b/src/test/resources/com/alipay/sofa/concurrency/log/logback/log-conf.xml @@ -0,0 +1,15 @@ + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + +