diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a696e3886..663a7d4bf 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -46,4 +46,4 @@ jobs: - name: "Codecov" uses: codecov/codecov-action@v3.1.0 with: - files: ./address/target/site/jacoco/jacoco.xml,./config/target/site/jacoco/jacoco.xml,./api/target/site/jacoco/jacoco.xml,./auth/target/site/jacoco/jacoco.xml,./client/target/site/jacoco/jacoco.xml,./common/target/site/jacoco/jacoco.xml,./consistency/target/site/jacoco/jacoco.xml,./console/target/site/jacoco/jacoco.xml,./core/target/site/jacoco/jacoco.xml,./naming/target/site/jacoco/jacoco.xml,./persistence/target/site/jacoco/jacoco.xml,./plugin-default-impl/nacos-default-auth-plugin/target/site/jacoco/jacoco.xml,./plugin/auth/target/site/jacoco/jacoco.xml,./plugin/config/target/site/jacoco/jacoco.xml,./plugin/control/target/site/jacoco/jacoco.xml,./plugin/datasource/target/site/jacoco/jacoco.xml,./plugin/encryption/target/site/jacoco/jacoco.xml,./plugin/environment/target/site/jacoco/jacoco.xml,./plugin/trace/target/site/jacoco/jacoco.xml,./prometheus/target/site/jacoco/jacoco.xml,./sys/target/site/jacoco/jacoco.xml + files: ./address/target/site/jacoco/jacoco.xml,./api/target/site/jacoco/jacoco.xml,./auth/target/site/jacoco/jacoco.xml,./client/target/site/jacoco/jacoco.xml,./common/target/site/jacoco/jacoco.xml,./config/target/site/jacoco/jacoco.xml,./consistency/target/site/jacoco/jacoco.xml,./console/target/site/jacoco/jacoco.xml,./core/target/site/jacoco/jacoco.xml,./logger-adapter-impl/log4j2-adapter/target/site/jacoco/jacoco.xml,./logger-adapter-impl/logback-adapter-12/target/site/jacoco/jacoco.xml,./naming/target/site/jacoco/jacoco.xml,./persistence/target/site/jacoco/jacoco.xml,./plugin-default-impl/nacos-default-auth-plugin/target/site/jacoco/jacoco.xml,./plugin-default-impl/nacos-default-control-plugin/target/site/jacoco/jacoco.xml,./plugin/auth/target/site/jacoco/jacoco.xml,./plugin/config/target/site/jacoco/jacoco.xml,./plugin/control/target/site/jacoco/jacoco.xml,./plugin/datasource/target/site/jacoco/jacoco.xml,./plugin/encryption/target/site/jacoco/jacoco.xml,./plugin/environment/target/site/jacoco/jacoco.xml,./plugin/trace/target/site/jacoco/jacoco.xml,./prometheus/target/site/jacoco/jacoco.xml,./sys/target/site/jacoco/jacoco.xml diff --git a/client/pom.xml b/client/pom.xml index d35bf55e3..2098ee5e4 100644 --- a/client/pom.xml +++ b/client/pom.xml @@ -39,25 +39,12 @@ slf4j-api true - + - org.apache.logging.log4j - log4j-core + ${project.groupId} + nacos-api true - - - org.apache.logging.log4j - log4j-api - true - - - - org.apache.logging.log4j - log4j-slf4j-impl - true - - ${project.groupId} nacos-common @@ -75,19 +62,32 @@ - ${project.groupId} - nacos-api + com.alibaba.nacos + nacos-logback-adapter-12 + ${project.version} + + + + com.alibaba.nacos + nacos-log4j2-adapter + ${project.version} + + + + org.apache.logging.log4j + log4j-core true + - ch.qos.logback - logback-classic + org.apache.logging.log4j + log4j-api true - + - ch.qos.logback - logback-core + org.apache.logging.log4j + log4j-slf4j-impl true diff --git a/client/src/main/java/com/alibaba/nacos/client/logging/NacosLogging.java b/client/src/main/java/com/alibaba/nacos/client/logging/NacosLogging.java index ab0e0ddcd..45c231d50 100644 --- a/client/src/main/java/com/alibaba/nacos/client/logging/NacosLogging.java +++ b/client/src/main/java/com/alibaba/nacos/client/logging/NacosLogging.java @@ -16,8 +16,12 @@ package com.alibaba.nacos.client.logging; +import com.alibaba.nacos.client.env.NacosClientProperties; import com.alibaba.nacos.common.executor.ExecutorFactory; import com.alibaba.nacos.common.executor.NameThreadFactory; +import com.alibaba.nacos.common.logging.NacosLoggingAdapter; +import com.alibaba.nacos.common.logging.NacosLoggingAdapterBuilder; +import com.alibaba.nacos.common.logging.NacosLoggingProperties; import com.alibaba.nacos.common.spi.NacosServiceLoader; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -44,13 +48,15 @@ public class NacosLogging { private void initLoggingAdapter() { Class loggerClass = LOGGER.getClass(); - for (NacosLoggingAdapter each : NacosServiceLoader.load(NacosLoggingAdapter.class)) { - LOGGER.info("Nacos Logging Adapter: {}", each.getClass().getName()); - if (each.isEnabled() && each.isAdaptedLogger(loggerClass)) { - LOGGER.info("Nacos Logging Adapter: {} match {} success.", each.getClass().getName(), + for (NacosLoggingAdapterBuilder each : NacosServiceLoader.load(NacosLoggingAdapterBuilder.class)) { + LOGGER.info("Nacos Logging Adapter Builder: {}", each.getClass().getName()); + NacosLoggingAdapter tempLoggingAdapter = buildLoggingAdapterFromBuilder(each); + if (isAdaptLogging(tempLoggingAdapter, loggerClass)) { + LOGGER.info("Nacos Logging Adapter: {} match {} success.", tempLoggingAdapter.getClass().getName(), loggerClass.getName()); - loggingProperties = new NacosLoggingProperties(each.getDefaultConfigLocation()); - loggingAdapter = each; + loggingProperties = new NacosLoggingProperties(tempLoggingAdapter.getDefaultConfigLocation(), + NacosClientProperties.PROTOTYPE.asProperties()); + loggingAdapter = tempLoggingAdapter; } } if (null == loggingAdapter) { @@ -60,6 +66,19 @@ public class NacosLogging { scheduleReloadTask(); } + private NacosLoggingAdapter buildLoggingAdapterFromBuilder(NacosLoggingAdapterBuilder builder) { + try { + return builder.build(); + } catch (Throwable e) { + LOGGER.warn("Build Nacos Logging Adapter failed: {}", e.getMessage()); + return null; + } + } + + private boolean isAdaptLogging(NacosLoggingAdapter loggingAdapter, Class loggerClass) { + return null != loggingAdapter && loggingAdapter.isEnabled() && loggingAdapter.isAdaptedLogger(loggerClass); + } + private void scheduleReloadTask() { ScheduledExecutorService reloadContextService = ExecutorFactory.Managed .newSingleScheduledExecutorService("Nacos-Client", diff --git a/client/src/test/java/com/alibaba/nacos/client/logging/NacosLoggingTest.java b/client/src/test/java/com/alibaba/nacos/client/logging/NacosLoggingTest.java index 2b9759cf9..b2d94d424 100644 --- a/client/src/test/java/com/alibaba/nacos/client/logging/NacosLoggingTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/logging/NacosLoggingTest.java @@ -18,6 +18,8 @@ package com.alibaba.nacos.client.logging; +import com.alibaba.nacos.common.logging.NacosLoggingAdapter; +import com.alibaba.nacos.common.logging.NacosLoggingProperties; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -27,6 +29,7 @@ import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; import java.lang.reflect.Field; +import java.util.Properties; import static org.mockito.Mockito.doThrow; @@ -42,7 +45,7 @@ public class NacosLoggingTest { @Before public void setUp() throws NoSuchFieldException, IllegalAccessException { - loggingProperties = new NacosLoggingProperties(""); + loggingProperties = new NacosLoggingProperties("", new Properties()); instance = NacosLogging.getInstance(); Field loggingPropertiesField = NacosLogging.class.getDeclaredField("loggingProperties"); loggingPropertiesField.setAccessible(true); diff --git a/client/src/main/java/com/alibaba/nacos/client/logging/NacosLoggingAdapter.java b/common/src/main/java/com/alibaba/nacos/common/logging/NacosLoggingAdapter.java similarity index 97% rename from client/src/main/java/com/alibaba/nacos/client/logging/NacosLoggingAdapter.java rename to common/src/main/java/com/alibaba/nacos/common/logging/NacosLoggingAdapter.java index 5fb9f549f..365655e03 100644 --- a/client/src/main/java/com/alibaba/nacos/client/logging/NacosLoggingAdapter.java +++ b/common/src/main/java/com/alibaba/nacos/common/logging/NacosLoggingAdapter.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.alibaba.nacos.client.logging; +package com.alibaba.nacos.common.logging; /** * Nacos client logging adapter. diff --git a/client/src/main/java/com/alibaba/nacos/client/logging/logback/NacosLogbackConfigurator.java b/common/src/main/java/com/alibaba/nacos/common/logging/NacosLoggingAdapterBuilder.java similarity index 57% rename from client/src/main/java/com/alibaba/nacos/client/logging/logback/NacosLogbackConfigurator.java rename to common/src/main/java/com/alibaba/nacos/common/logging/NacosLoggingAdapterBuilder.java index b3085c2cf..289684176 100644 --- a/client/src/main/java/com/alibaba/nacos/client/logging/logback/NacosLogbackConfigurator.java +++ b/common/src/main/java/com/alibaba/nacos/common/logging/NacosLoggingAdapterBuilder.java @@ -14,26 +14,26 @@ * limitations under the License. */ -package com.alibaba.nacos.client.logging.logback; - -import java.net.URL; +package com.alibaba.nacos.common.logging; /** - * logback configurator interface,different version can adapter this. - * @author hujun + * Builder of {@link NacosLoggingAdapter}. + * + *

+ * Why not directly SPI {@link NacosLoggingAdapter}? + *

+ *

+ * To avoid some {@link NacosLoggingAdapter} initialization error casue the SPI loader exit. + *

+ * + * @author xiweng.yy */ -public interface NacosLogbackConfigurator { +public interface NacosLoggingAdapterBuilder { /** - * config logback. - * @param resourceUrl resourceUrl - * @throws Exception exception + * Build {@link NacosLoggingAdapter} implementation. + * + * @return {@link NacosLoggingAdapter} */ - void configure(URL resourceUrl) throws Exception; - - /** - * set loggerContext. - * @param loggerContext loggerContext - */ - void setContext(Object loggerContext); + NacosLoggingAdapter build(); } diff --git a/client/src/main/java/com/alibaba/nacos/client/logging/NacosLoggingProperties.java b/common/src/main/java/com/alibaba/nacos/common/logging/NacosLoggingProperties.java similarity index 81% rename from client/src/main/java/com/alibaba/nacos/client/logging/NacosLoggingProperties.java rename to common/src/main/java/com/alibaba/nacos/common/logging/NacosLoggingProperties.java index bd2a96b0e..3a870abae 100644 --- a/client/src/main/java/com/alibaba/nacos/client/logging/NacosLoggingProperties.java +++ b/common/src/main/java/com/alibaba/nacos/common/logging/NacosLoggingProperties.java @@ -14,12 +14,13 @@ * limitations under the License. */ -package com.alibaba.nacos.client.logging; +package com.alibaba.nacos.common.logging; -import com.alibaba.nacos.client.env.NacosClientProperties; import com.alibaba.nacos.common.utils.ConvertUtils; import com.alibaba.nacos.common.utils.StringUtils; +import java.util.Properties; + /** * Nacos Logging Properties, save some nacos logging properties. * @@ -37,8 +38,11 @@ public class NacosLoggingProperties { private final String defaultLocation; - public NacosLoggingProperties(String defaultLocation) { + private final Properties properties; + + public NacosLoggingProperties(String defaultLocation, Properties properties) { this.defaultLocation = defaultLocation; + this.properties = null == properties ? new Properties() : properties; } /** @@ -47,7 +51,7 @@ public class NacosLoggingProperties { * @return location of nacos logging configuration */ public String getLocation() { - String location = NacosClientProperties.PROTOTYPE.getProperty(NACOS_LOGGING_CONFIG_PROPERTY); + String location = properties.getProperty(NACOS_LOGGING_CONFIG_PROPERTY); if (StringUtils.isBlank(location)) { if (isDefaultLocationEnabled()) { return defaultLocation; @@ -65,7 +69,7 @@ public class NacosLoggingProperties { * @return {@code true} if default location enabled, otherwise {@code false}, default is {@code true} */ private boolean isDefaultLocationEnabled() { - String property = NacosClientProperties.PROTOTYPE.getProperty(NACOS_LOGGING_DEFAULT_CONFIG_ENABLED_PROPERTY); + String property = properties.getProperty(NACOS_LOGGING_DEFAULT_CONFIG_ENABLED_PROPERTY); return property == null || ConvertUtils.toBoolean(property); } @@ -75,7 +79,7 @@ public class NacosLoggingProperties { * @return reload internal */ public long getReloadInternal() { - String interval = NacosClientProperties.PROTOTYPE.getProperty(NACOS_LOGGING_RELOAD_INTERVAL_PROPERTY); + String interval = properties.getProperty(NACOS_LOGGING_RELOAD_INTERVAL_PROPERTY); return ConvertUtils.toLong(interval, DEFAULT_NACOS_LOGGING_RELOAD_INTERVAL); } @@ -87,6 +91,6 @@ public class NacosLoggingProperties { * @return value */ public String getValue(String source, String defaultValue) { - return NacosClientProperties.PROTOTYPE.getProperty(source, defaultValue); + return properties.getProperty(source, defaultValue); } } diff --git a/common/src/test/java/com/alibaba/nacos/common/logging/NacosLoggingPropertiesTest.java b/common/src/test/java/com/alibaba/nacos/common/logging/NacosLoggingPropertiesTest.java new file mode 100644 index 000000000..e823da977 --- /dev/null +++ b/common/src/test/java/com/alibaba/nacos/common/logging/NacosLoggingPropertiesTest.java @@ -0,0 +1,76 @@ +/* + * Copyright 1999-2023 Alibaba Group Holding Ltd. + * + * Licensed 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.alibaba.nacos.common.logging; + +import org.junit.Before; +import org.junit.Test; + +import java.util.Properties; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +public class NacosLoggingPropertiesTest { + + NacosLoggingProperties loggingProperties; + + Properties properties; + + @Before + public void setUp() throws Exception { + properties = new Properties(); + loggingProperties = new NacosLoggingProperties("classpath:test.xml", properties); + } + + @Test + public void testGetLocationWithDefault() { + assertEquals("classpath:test.xml", loggingProperties.getLocation()); + } + + @Test + public void testGetLocationWithoutDefault() { + properties.setProperty("nacos.logging.default.config.enabled", "false"); + assertNull(loggingProperties.getLocation()); + } + + @Test + public void testGetLocationForSpecified() { + properties.setProperty("nacos.logging.config", "classpath:specified-test.xml"); + properties.setProperty("nacos.logging.default.config.enabled", "false"); + assertEquals("classpath:specified-test.xml", loggingProperties.getLocation()); + } + + @Test + public void testGetLocationForSpecifiedWithDefault() { + properties.setProperty("nacos.logging.config", "classpath:specified-test.xml"); + assertEquals("classpath:specified-test.xml", loggingProperties.getLocation()); + } + + @Test + public void testGetReloadInternal() { + properties.setProperty("nacos.logging.reload.interval.seconds", "50000"); + assertEquals(50000L, loggingProperties.getReloadInternal()); + } + + @Test + public void testGetValue() { + properties.setProperty("test.key", "test.value"); + assertEquals("test.value", loggingProperties.getValue("test.key", "default.value")); + properties.clear(); + assertEquals("default.value", loggingProperties.getValue("test.key", "default.value")); + } +} \ No newline at end of file diff --git a/core/src/test/java/com/alibaba/nacos/core/remote/ConnectionManagerTest.java b/core/src/test/java/com/alibaba/nacos/core/remote/ConnectionManagerTest.java index d33b95f0d..622c6bb45 100644 --- a/core/src/test/java/com/alibaba/nacos/core/remote/ConnectionManagerTest.java +++ b/core/src/test/java/com/alibaba/nacos/core/remote/ConnectionManagerTest.java @@ -35,6 +35,7 @@ import org.mockito.Mock; import org.mockito.MockedStatic; import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; +import org.springframework.mock.env.MockEnvironment; import java.io.File; import java.util.HashMap; @@ -87,6 +88,7 @@ public class ConnectionManagerTest { @Before public void setUp() { + EnvUtil.setEnvironment(new MockEnvironment()); // create base file path File baseDir = new File(EnvUtil.getNacosHome(), "data"); if (!baseDir.exists()) { diff --git a/logger-adapter-impl/log4j2-adapter/pom.xml b/logger-adapter-impl/log4j2-adapter/pom.xml new file mode 100644 index 000000000..ac410ad34 --- /dev/null +++ b/logger-adapter-impl/log4j2-adapter/pom.xml @@ -0,0 +1,55 @@ + + + + + + nacos-logger-adapter-impl + com.alibaba.nacos + ${revision} + + 4.0.0 + + nacos-log4j2-adapter + nacos-log4j2-adapter ${revision} + Nacos client log4j2 adapter pom.xml file + + + + org.apache.logging.log4j + log4j-core + provided + true + + + + org.apache.logging.log4j + log4j-api + provided + true + + + + org.apache.logging.log4j + log4j-slf4j-impl + provided + true + + + + \ No newline at end of file diff --git a/client/src/main/java/com/alibaba/nacos/client/logging/log4j2/Log4J2NacosLoggingAdapter.java b/logger-adapter-impl/log4j2-adapter/src/main/java/com/alibaba/nacos/logger/adapter/log4j2/Log4J2NacosLoggingAdapter.java similarity index 94% rename from client/src/main/java/com/alibaba/nacos/client/logging/log4j2/Log4J2NacosLoggingAdapter.java rename to logger-adapter-impl/log4j2-adapter/src/main/java/com/alibaba/nacos/logger/adapter/log4j2/Log4J2NacosLoggingAdapter.java index 3e2382c3f..758e95385 100644 --- a/client/src/main/java/com/alibaba/nacos/client/logging/log4j2/Log4J2NacosLoggingAdapter.java +++ b/logger-adapter-impl/log4j2-adapter/src/main/java/com/alibaba/nacos/logger/adapter/log4j2/Log4J2NacosLoggingAdapter.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2018 Alibaba Group Holding Ltd. + * Copyright 1999-2023 Alibaba Group Holding Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,10 +14,10 @@ * limitations under the License. */ -package com.alibaba.nacos.client.logging.log4j2; +package com.alibaba.nacos.logger.adapter.log4j2; -import com.alibaba.nacos.client.logging.NacosLoggingAdapter; -import com.alibaba.nacos.client.logging.NacosLoggingProperties; +import com.alibaba.nacos.common.logging.NacosLoggingAdapter; +import com.alibaba.nacos.common.logging.NacosLoggingProperties; import com.alibaba.nacos.common.utils.ResourceUtils; import com.alibaba.nacos.common.utils.StringUtils; import org.apache.logging.log4j.LogManager; @@ -87,6 +87,7 @@ public class Log4J2NacosLoggingAdapter implements NacosLoggingAdapter { @Override public void loadConfiguration(NacosLoggingProperties loggingProperties) { + Log4j2NacosLoggingPropertiesHolder.setProperties(loggingProperties); String location = loggingProperties.getLocation(); loadConfiguration(location); } diff --git a/logger-adapter-impl/log4j2-adapter/src/main/java/com/alibaba/nacos/logger/adapter/log4j2/Log4j2NacosLoggingAdapterBuilder.java b/logger-adapter-impl/log4j2-adapter/src/main/java/com/alibaba/nacos/logger/adapter/log4j2/Log4j2NacosLoggingAdapterBuilder.java new file mode 100644 index 000000000..f7453e526 --- /dev/null +++ b/logger-adapter-impl/log4j2-adapter/src/main/java/com/alibaba/nacos/logger/adapter/log4j2/Log4j2NacosLoggingAdapterBuilder.java @@ -0,0 +1,33 @@ +/* + * Copyright 1999-2023 Alibaba Group Holding Ltd. + * + * Licensed 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.alibaba.nacos.logger.adapter.log4j2; + +import com.alibaba.nacos.common.logging.NacosLoggingAdapter; +import com.alibaba.nacos.common.logging.NacosLoggingAdapterBuilder; + +/** + * Builder of {@link com.alibaba.nacos.common.logging.NacosLoggingAdapter} for log4j2. + * + * @author xiweng.yy + */ +public class Log4j2NacosLoggingAdapterBuilder implements NacosLoggingAdapterBuilder { + + @Override + public NacosLoggingAdapter build() { + return new Log4J2NacosLoggingAdapter(); + } +} diff --git a/logger-adapter-impl/log4j2-adapter/src/main/java/com/alibaba/nacos/logger/adapter/log4j2/Log4j2NacosLoggingPropertiesHolder.java b/logger-adapter-impl/log4j2-adapter/src/main/java/com/alibaba/nacos/logger/adapter/log4j2/Log4j2NacosLoggingPropertiesHolder.java new file mode 100644 index 000000000..ccdb187bd --- /dev/null +++ b/logger-adapter-impl/log4j2-adapter/src/main/java/com/alibaba/nacos/logger/adapter/log4j2/Log4j2NacosLoggingPropertiesHolder.java @@ -0,0 +1,46 @@ +/* + * Copyright 1999-2023 Alibaba Group Holding Ltd. + * + * Licensed 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.alibaba.nacos.logger.adapter.log4j2; + +import com.alibaba.nacos.common.logging.NacosLoggingProperties; +import org.apache.logging.log4j.core.config.plugins.Plugin; + +/** + * Log4j2 nacos logging properties holder. + * + *

+ * Log4j2 use {@link Plugin} annotation to inject Properties lookup to set properties, this holder is to keep the {@link + * NacosLoggingProperties} and called in {@link NacosClientPropertiesLookup} which is annotate by {@link Plugin}. + *

+ * + * @author xiweng.yy + */ +public class Log4j2NacosLoggingPropertiesHolder { + + private static final Log4j2NacosLoggingPropertiesHolder INSTANCE = new Log4j2NacosLoggingPropertiesHolder(); + + private NacosLoggingProperties properties; + + public static void setProperties(NacosLoggingProperties properties) { + INSTANCE.properties = properties; + } + + public static String getValue(String key) { + return null == INSTANCE.properties ? null : INSTANCE.properties.getValue(key, null); + } + +} diff --git a/client/src/main/java/com/alibaba/nacos/client/logging/log4j2/NacosClientPropertiesLookup.java b/logger-adapter-impl/log4j2-adapter/src/main/java/com/alibaba/nacos/logger/adapter/log4j2/NacosClientPropertiesLookup.java similarity index 67% rename from client/src/main/java/com/alibaba/nacos/client/logging/log4j2/NacosClientPropertiesLookup.java rename to logger-adapter-impl/log4j2-adapter/src/main/java/com/alibaba/nacos/logger/adapter/log4j2/NacosClientPropertiesLookup.java index 43068aa26..3070a69fa 100644 --- a/client/src/main/java/com/alibaba/nacos/client/logging/log4j2/NacosClientPropertiesLookup.java +++ b/logger-adapter-impl/log4j2-adapter/src/main/java/com/alibaba/nacos/logger/adapter/log4j2/NacosClientPropertiesLookup.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2022 Alibaba Group Holding Ltd. + * Copyright 1999-2023 Alibaba Group Holding Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,25 +14,24 @@ * limitations under the License. */ -package com.alibaba.nacos.client.logging.log4j2; +package com.alibaba.nacos.logger.adapter.log4j2; -import com.alibaba.nacos.client.env.NacosClientProperties; import org.apache.logging.log4j.core.LogEvent; import org.apache.logging.log4j.core.config.plugins.Plugin; import org.apache.logging.log4j.core.lookup.AbstractLookup; import org.apache.logging.log4j.core.lookup.StrLookup; /** - * support log4j2 read properties from NacosClientProperties. - * for example: - * + * support log4j2 read properties from NacosClientProperties. for example: + * + * * @author onewe */ @Plugin(name = "nacosClientProperty", category = StrLookup.CATEGORY) -public class NacosClientPropertiesLookup extends AbstractLookup { +public class NacosClientPropertiesLookup extends AbstractLookup { @Override public String lookup(LogEvent event, String key) { - return NacosClientProperties.PROTOTYPE.getProperty(key); + return Log4j2NacosLoggingPropertiesHolder.getValue(key); } } \ No newline at end of file diff --git a/client/src/main/resources/META-INF/services/com.alibaba.nacos.client.logging.NacosLoggingAdapter b/logger-adapter-impl/log4j2-adapter/src/main/resources/META-INF/services/com.alibaba.nacos.common.logging.NacosLoggingAdapterBuilder similarity index 81% rename from client/src/main/resources/META-INF/services/com.alibaba.nacos.client.logging.NacosLoggingAdapter rename to logger-adapter-impl/log4j2-adapter/src/main/resources/META-INF/services/com.alibaba.nacos.common.logging.NacosLoggingAdapterBuilder index 4d8c82c4f..33389b66e 100644 --- a/client/src/main/resources/META-INF/services/com.alibaba.nacos.client.logging.NacosLoggingAdapter +++ b/logger-adapter-impl/log4j2-adapter/src/main/resources/META-INF/services/com.alibaba.nacos.common.logging.NacosLoggingAdapterBuilder @@ -14,5 +14,4 @@ # limitations under the License. # -com.alibaba.nacos.client.logging.logback.LogbackNacosLoggingAdapter -com.alibaba.nacos.client.logging.log4j2.Log4J2NacosLoggingAdapter \ No newline at end of file +com.alibaba.nacos.logger.adapter.log4j2.Log4j2NacosLoggingAdapterBuilder \ No newline at end of file diff --git a/client/src/main/resources/nacos-log4j2.xml b/logger-adapter-impl/log4j2-adapter/src/main/resources/nacos-log4j2.xml similarity index 100% rename from client/src/main/resources/nacos-log4j2.xml rename to logger-adapter-impl/log4j2-adapter/src/main/resources/nacos-log4j2.xml diff --git a/client/src/test/java/com/alibaba/nacos/client/logging/log4j2/Log4J2NacosLoggingAdapterTest.java b/logger-adapter-impl/log4j2-adapter/src/test/java/com/alibaba/nacos/logger/adapter/log4j2/Log4J2NacosLoggingAdapterTest.java similarity index 91% rename from client/src/test/java/com/alibaba/nacos/client/logging/log4j2/Log4J2NacosLoggingAdapterTest.java rename to logger-adapter-impl/log4j2-adapter/src/test/java/com/alibaba/nacos/logger/adapter/log4j2/Log4J2NacosLoggingAdapterTest.java index a0e0fe09a..1c3e1a70e 100644 --- a/client/src/test/java/com/alibaba/nacos/client/logging/log4j2/Log4J2NacosLoggingAdapterTest.java +++ b/logger-adapter-impl/log4j2-adapter/src/test/java/com/alibaba/nacos/logger/adapter/log4j2/Log4J2NacosLoggingAdapterTest.java @@ -1,6 +1,5 @@ /* - * - * Copyright 1999-2018 Alibaba Group Holding Ltd. + * Copyright 1999-2023 Alibaba Group Holding Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,12 +12,11 @@ * 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.alibaba.nacos.client.logging.log4j2; +package com.alibaba.nacos.logger.adapter.log4j2; -import com.alibaba.nacos.client.logging.NacosLoggingProperties; +import com.alibaba.nacos.common.logging.NacosLoggingProperties; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.core.LoggerContext; import org.apache.logging.log4j.core.config.Configuration; @@ -39,6 +37,7 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.URL; import java.util.Map; +import java.util.logging.Logger; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -64,7 +63,7 @@ public class Log4J2NacosLoggingAdapterTest { @Before public void setUp() throws Exception { log4J2NacosLoggingAdapter = new Log4J2NacosLoggingAdapter(); - nacosLoggingProperties = new NacosLoggingProperties("classpath:nacos-log4j2.xml"); + nacosLoggingProperties = new NacosLoggingProperties("classpath:nacos-log4j2.xml", System.getProperties()); LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false); loggerContext.addPropertyChangeListener(propertyChangeListener); } @@ -81,7 +80,7 @@ public class Log4J2NacosLoggingAdapterTest { @Test public void testIsAdaptedLogger() { assertTrue(log4J2NacosLoggingAdapter.isAdaptedLogger(org.apache.logging.slf4j.Log4jLogger.class)); - assertFalse(log4J2NacosLoggingAdapter.isAdaptedLogger(ch.qos.logback.classic.Logger.class)); + assertFalse(log4J2NacosLoggingAdapter.isAdaptedLogger(Logger.class)); } @Test @@ -117,6 +116,7 @@ public class Log4J2NacosLoggingAdapterTest { @Test public void testLoadConfigurationWithoutLocation() { System.setProperty("nacos.logging.default.config.enabled", "false"); + nacosLoggingProperties = new NacosLoggingProperties("classpath:nacos-log4j2.xml", System.getProperties()); log4J2NacosLoggingAdapter = new Log4J2NacosLoggingAdapter(); log4J2NacosLoggingAdapter.loadConfiguration(nacosLoggingProperties); verify(propertyChangeListener, never()).propertyChange(any()); @@ -125,6 +125,7 @@ public class Log4J2NacosLoggingAdapterTest { @Test(expected = IllegalStateException.class) public void testLoadConfigurationWithWrongLocation() { System.setProperty("nacos.logging.config", "http://localhost"); + nacosLoggingProperties = new NacosLoggingProperties("classpath:nacos-log4j2.xml", System.getProperties()); log4J2NacosLoggingAdapter = new Log4J2NacosLoggingAdapter(); log4J2NacosLoggingAdapter.loadConfiguration(nacosLoggingProperties); verify(propertyChangeListener, never()).propertyChange(any()); diff --git a/logger-adapter-impl/log4j2-adapter/src/test/java/com/alibaba/nacos/logger/adapter/log4j2/Log4j2NacosLoggingAdapterBuilderTest.java b/logger-adapter-impl/log4j2-adapter/src/test/java/com/alibaba/nacos/logger/adapter/log4j2/Log4j2NacosLoggingAdapterBuilderTest.java new file mode 100644 index 000000000..a8db92239 --- /dev/null +++ b/logger-adapter-impl/log4j2-adapter/src/test/java/com/alibaba/nacos/logger/adapter/log4j2/Log4j2NacosLoggingAdapterBuilderTest.java @@ -0,0 +1,34 @@ +/* + * Copyright 1999-2023 Alibaba Group Holding Ltd. + * + * Licensed 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.alibaba.nacos.logger.adapter.log4j2; + +import com.alibaba.nacos.common.logging.NacosLoggingAdapter; +import org.junit.Test; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +public class Log4j2NacosLoggingAdapterBuilderTest { + + @Test + public void build() { + Log4j2NacosLoggingAdapterBuilder builder = new Log4j2NacosLoggingAdapterBuilder(); + NacosLoggingAdapter adapter = builder.build(); + assertNotNull(adapter); + assertTrue(adapter instanceof Log4J2NacosLoggingAdapter); + } +} \ No newline at end of file diff --git a/client/src/test/java/com/alibaba/nacos/client/logging/log4j2/NacosClientPropertiesLookupTest.java b/logger-adapter-impl/log4j2-adapter/src/test/java/com/alibaba/nacos/logger/adapter/log4j2/NacosClientPropertiesLookupTest.java similarity index 70% rename from client/src/test/java/com/alibaba/nacos/client/logging/log4j2/NacosClientPropertiesLookupTest.java rename to logger-adapter-impl/log4j2-adapter/src/test/java/com/alibaba/nacos/logger/adapter/log4j2/NacosClientPropertiesLookupTest.java index bdc5f36cd..d193ac2d6 100644 --- a/client/src/test/java/com/alibaba/nacos/client/logging/log4j2/NacosClientPropertiesLookupTest.java +++ b/logger-adapter-impl/log4j2-adapter/src/test/java/com/alibaba/nacos/logger/adapter/log4j2/NacosClientPropertiesLookupTest.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2022 Alibaba Group Holding Ltd. + * Copyright 1999-2023 Alibaba Group Holding Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,9 +14,9 @@ * limitations under the License. */ -package com.alibaba.nacos.client.logging.log4j2; +package com.alibaba.nacos.logger.adapter.log4j2; -import com.alibaba.nacos.client.env.NacosClientProperties; +import com.alibaba.nacos.common.logging.NacosLoggingProperties; import org.junit.Assert; import org.junit.Test; @@ -24,7 +24,9 @@ public class NacosClientPropertiesLookupTest { @Test public void testLookUp() { - NacosClientProperties.PROTOTYPE.setProperty("test.nacos.logging.lookup", "true"); + System.setProperty("test.nacos.logging.lookup", "true"); + NacosLoggingProperties properties = new NacosLoggingProperties("", System.getProperties()); + Log4j2NacosLoggingPropertiesHolder.setProperties(properties); NacosClientPropertiesLookup nacosClientPropertiesLookup = new NacosClientPropertiesLookup(); final String actual = nacosClientPropertiesLookup.lookup("test.nacos.logging.lookup"); Assert.assertEquals("true", actual); diff --git a/logger-adapter-impl/logback-adapter-12/pom.xml b/logger-adapter-impl/logback-adapter-12/pom.xml new file mode 100644 index 000000000..c0166d9ed --- /dev/null +++ b/logger-adapter-impl/logback-adapter-12/pom.xml @@ -0,0 +1,58 @@ + + + + + + nacos-logger-adapter-impl + com.alibaba.nacos + ${revision} + + 4.0.0 + + nacos-logback-adapter-12 + nacos-logback-adapter-12 ${revision} + Nacos client logback adapter for logback 1.2.x and lower version pom.xml file + + + + org.slf4j + slf4j-api + 1.7.26 + provided + true + + + + ch.qos.logback + logback-classic + 1.2.13 + provided + true + + + + ch.qos.logback + logback-core + 1.2.13 + provided + true + + + + \ No newline at end of file diff --git a/client/src/main/java/com/alibaba/nacos/client/logging/logback/LogbackNacosLoggingAdapter.java b/logger-adapter-impl/logback-adapter-12/src/main/java/com/alibaba/nacos/logger/adapter/logback12/LogbackNacosLoggingAdapter.java similarity index 92% rename from client/src/main/java/com/alibaba/nacos/client/logging/logback/LogbackNacosLoggingAdapter.java rename to logger-adapter-impl/logback-adapter-12/src/main/java/com/alibaba/nacos/logger/adapter/logback12/LogbackNacosLoggingAdapter.java index 982893567..75aa47405 100644 --- a/client/src/main/java/com/alibaba/nacos/client/logging/logback/LogbackNacosLoggingAdapter.java +++ b/logger-adapter-impl/logback-adapter-12/src/main/java/com/alibaba/nacos/logger/adapter/logback12/LogbackNacosLoggingAdapter.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2018 Alibaba Group Holding Ltd. + * Copyright 1999-2023 Alibaba Group Holding Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,15 +14,15 @@ * limitations under the License. */ -package com.alibaba.nacos.client.logging.logback; +package com.alibaba.nacos.logger.adapter.logback12; import ch.qos.logback.classic.Level; import ch.qos.logback.classic.Logger; import ch.qos.logback.classic.LoggerContext; import ch.qos.logback.classic.spi.LoggerContextListener; import ch.qos.logback.core.CoreConstants; -import com.alibaba.nacos.client.logging.NacosLoggingAdapter; -import com.alibaba.nacos.client.logging.NacosLoggingProperties; +import com.alibaba.nacos.common.logging.NacosLoggingAdapter; +import com.alibaba.nacos.common.logging.NacosLoggingProperties; import com.alibaba.nacos.common.utils.ResourceUtils; import com.alibaba.nacos.common.utils.StringUtils; import org.slf4j.LoggerFactory; @@ -37,15 +37,12 @@ import org.slf4j.LoggerFactory; */ public class LogbackNacosLoggingAdapter implements NacosLoggingAdapter { - private static final String NACOS_LOGBACK_LOCATION = "classpath:nacos-logback.xml"; + private static final String NACOS_LOGBACK_LOCATION = "classpath:nacos-logback12.xml"; private static final String LOGBACK_CLASSES = "ch.qos.logback.classic.Logger"; - private final NacosLogbackConfigurator configurator; + private final NacosLogbackConfiguratorAdapterV1 configurator; - /** - * logback use 'ch.qos.logback.core.model.Model' since 1.3.0, set logback version during initialization. - */ public LogbackNacosLoggingAdapter() { configurator = new NacosLogbackConfiguratorAdapterV1(); } @@ -67,6 +64,9 @@ public class LogbackNacosLoggingAdapter implements NacosLoggingAdapter { } } + /** + * logback use 'ch.qos.logback.core.model.Model' since 1.3.0, set logback version during initialization. + */ private boolean isUpperLogback13() { try { Class.forName("ch.qos.logback.core.model.Model"); @@ -89,6 +89,7 @@ public class LogbackNacosLoggingAdapter implements NacosLoggingAdapter { @Override public void loadConfiguration(NacosLoggingProperties loggingProperties) { String location = loggingProperties.getLocation(); + configurator.setLoggingProperties(loggingProperties); LoggerContext loggerContext = loadConfigurationOnStart(location); if (loggerContext.getObject(CoreConstants.RECONFIGURE_ON_CHANGE_TASK) != null && hasNoListener(loggerContext)) { addListener(loggerContext, location); diff --git a/logger-adapter-impl/logback-adapter-12/src/main/java/com/alibaba/nacos/logger/adapter/logback12/LogbackNacosLoggingAdapterBuilder.java b/logger-adapter-impl/logback-adapter-12/src/main/java/com/alibaba/nacos/logger/adapter/logback12/LogbackNacosLoggingAdapterBuilder.java new file mode 100644 index 000000000..252d2bb20 --- /dev/null +++ b/logger-adapter-impl/logback-adapter-12/src/main/java/com/alibaba/nacos/logger/adapter/logback12/LogbackNacosLoggingAdapterBuilder.java @@ -0,0 +1,33 @@ +/* + * Copyright 1999-2023 Alibaba Group Holding Ltd. + * + * Licensed 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.alibaba.nacos.logger.adapter.logback12; + +import com.alibaba.nacos.common.logging.NacosLoggingAdapter; +import com.alibaba.nacos.common.logging.NacosLoggingAdapterBuilder; + +/** + * Builder of {@link com.alibaba.nacos.common.logging.NacosLoggingAdapter} for logback 1.2.x and below. + * + * @author xiweng.yy + */ +public class LogbackNacosLoggingAdapterBuilder implements NacosLoggingAdapterBuilder { + + @Override + public NacosLoggingAdapter build() { + return new LogbackNacosLoggingAdapter(); + } +} diff --git a/client/src/main/java/com/alibaba/nacos/client/logging/logback/NacosClientPropertyAction.java b/logger-adapter-impl/logback-adapter-12/src/main/java/com/alibaba/nacos/logger/adapter/logback12/NacosClientPropertyAction.java similarity index 80% rename from client/src/main/java/com/alibaba/nacos/client/logging/logback/NacosClientPropertyAction.java rename to logger-adapter-impl/logback-adapter-12/src/main/java/com/alibaba/nacos/logger/adapter/logback12/NacosClientPropertyAction.java index 8031d35ea..4a02d838f 100644 --- a/client/src/main/java/com/alibaba/nacos/client/logging/logback/NacosClientPropertyAction.java +++ b/logger-adapter-impl/logback-adapter-12/src/main/java/com/alibaba/nacos/logger/adapter/logback12/NacosClientPropertyAction.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2022 Alibaba Group Holding Ltd. + * Copyright 1999-2023 Alibaba Group Holding Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,20 +14,20 @@ * limitations under the License. */ -package com.alibaba.nacos.client.logging.logback; +package com.alibaba.nacos.logger.adapter.logback12; import ch.qos.logback.core.joran.action.Action; import ch.qos.logback.core.joran.action.ActionUtil; import ch.qos.logback.core.joran.spi.ActionException; import ch.qos.logback.core.joran.spi.InterpretationContext; import ch.qos.logback.core.util.OptionHelper; -import com.alibaba.nacos.client.env.NacosClientProperties; +import com.alibaba.nacos.common.logging.NacosLoggingProperties; import org.xml.sax.Attributes; /** - * support logback read properties from NacosClientProperties. just like springProperty. - * for example: + * support logback read properties from NacosClientProperties. just like springProperty. for example: * + * * @author onewe */ class NacosClientPropertyAction extends Action { @@ -36,6 +36,12 @@ class NacosClientPropertyAction extends Action { private static final String SOURCE_ATTRIBUTE = "source"; + private final NacosLoggingProperties loggingProperties; + + NacosClientPropertyAction(NacosLoggingProperties loggingProperties) { + this.loggingProperties = loggingProperties; + } + @Override public void begin(InterpretationContext ic, String elementName, Attributes attributes) throws ActionException { String name = attributes.getValue(NAME_ATTRIBUTE); @@ -54,6 +60,6 @@ class NacosClientPropertyAction extends Action { } private String getValue(String source, String defaultValue) { - return NacosClientProperties.PROTOTYPE.getProperty(source, defaultValue); + return null == loggingProperties ? defaultValue : loggingProperties.getValue(source, defaultValue); } } \ No newline at end of file diff --git a/client/src/main/java/com/alibaba/nacos/client/logging/logback/NacosLogbackConfiguratorAdapterV1.java b/logger-adapter-impl/logback-adapter-12/src/main/java/com/alibaba/nacos/logger/adapter/logback12/NacosLogbackConfiguratorAdapterV1.java similarity index 85% rename from client/src/main/java/com/alibaba/nacos/client/logging/logback/NacosLogbackConfiguratorAdapterV1.java rename to logger-adapter-impl/logback-adapter-12/src/main/java/com/alibaba/nacos/logger/adapter/logback12/NacosLogbackConfiguratorAdapterV1.java index e8b16358c..cc9e69e89 100644 --- a/client/src/main/java/com/alibaba/nacos/client/logging/logback/NacosLogbackConfiguratorAdapterV1.java +++ b/logger-adapter-impl/logback-adapter-12/src/main/java/com/alibaba/nacos/logger/adapter/logback12/NacosLogbackConfiguratorAdapterV1.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2018 Alibaba Group Holding Ltd. + * Copyright 1999-2023 Alibaba Group Holding Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,14 +14,14 @@ * limitations under the License. */ -package com.alibaba.nacos.client.logging.logback; +package com.alibaba.nacos.logger.adapter.logback12; import ch.qos.logback.classic.joran.JoranConfigurator; -import ch.qos.logback.core.Context; import ch.qos.logback.core.joran.event.SaxEvent; import ch.qos.logback.core.joran.spi.ElementSelector; import ch.qos.logback.core.joran.spi.JoranException; import ch.qos.logback.core.joran.spi.RuleStore; +import com.alibaba.nacos.common.logging.NacosLoggingProperties; import java.io.IOException; import java.io.InputStream; @@ -35,7 +35,13 @@ import java.util.List; * @author hujun * @see #6999 */ -public class NacosLogbackConfiguratorAdapterV1 extends JoranConfigurator implements NacosLogbackConfigurator { +public class NacosLogbackConfiguratorAdapterV1 extends JoranConfigurator { + + private NacosLoggingProperties loggingProperties; + + public void setLoggingProperties(NacosLoggingProperties loggingProperties) { + this.loggingProperties = loggingProperties; + } /** * ensure that Nacos configuration does not affect user configuration savepoints. @@ -49,12 +55,8 @@ public class NacosLogbackConfiguratorAdapterV1 extends JoranConfigurator impleme @Override public void addInstanceRules(RuleStore rs) { super.addInstanceRules(rs); - rs.addRule(new ElementSelector("configuration/nacosClientProperty"), new NacosClientPropertyAction()); - } - - @Override - public void setContext(Object loggerContext) { - super.setContext((Context) loggerContext); + rs.addRule(new ElementSelector("configuration/nacosClientProperty"), + new NacosClientPropertyAction(loggingProperties)); } /** @@ -63,7 +65,6 @@ public class NacosLogbackConfiguratorAdapterV1 extends JoranConfigurator impleme * @param url config url * @throws Exception e */ - @Override public void configure(URL url) throws Exception { InputStream in = null; try { diff --git a/logger-adapter-impl/logback-adapter-12/src/main/resources/META-INF/services/com.alibaba.nacos.common.logging.NacosLoggingAdapter b/logger-adapter-impl/logback-adapter-12/src/main/resources/META-INF/services/com.alibaba.nacos.common.logging.NacosLoggingAdapter new file mode 100644 index 000000000..36efc9371 --- /dev/null +++ b/logger-adapter-impl/logback-adapter-12/src/main/resources/META-INF/services/com.alibaba.nacos.common.logging.NacosLoggingAdapter @@ -0,0 +1,17 @@ +# +# Copyright 1999-2023 Alibaba Group Holding Ltd. +# +# Licensed 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. +# + +com.alibaba.nacos.logger.adapter.logback12.LogbackNacosLoggingAdapter \ No newline at end of file diff --git a/logger-adapter-impl/logback-adapter-12/src/main/resources/META-INF/services/com.alibaba.nacos.common.logging.NacosLoggingAdapterBuilder b/logger-adapter-impl/logback-adapter-12/src/main/resources/META-INF/services/com.alibaba.nacos.common.logging.NacosLoggingAdapterBuilder new file mode 100644 index 000000000..2084f5d62 --- /dev/null +++ b/logger-adapter-impl/logback-adapter-12/src/main/resources/META-INF/services/com.alibaba.nacos.common.logging.NacosLoggingAdapterBuilder @@ -0,0 +1,17 @@ +# +# Copyright 1999-2023 Alibaba Group Holding Ltd. +# +# Licensed 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. +# + +com.alibaba.nacos.logger.adapter.logback12.LogbackNacosLoggingAdapterBuilder \ No newline at end of file diff --git a/client/src/main/resources/nacos-logback.xml b/logger-adapter-impl/logback-adapter-12/src/main/resources/nacos-logback12.xml similarity index 100% rename from client/src/main/resources/nacos-logback.xml rename to logger-adapter-impl/logback-adapter-12/src/main/resources/nacos-logback12.xml diff --git a/logger-adapter-impl/logback-adapter-12/src/test/java/com/alibaba/nacos/logger/adapter/logback12/LogbackNacosLoggingAdapterBuilderTest.java b/logger-adapter-impl/logback-adapter-12/src/test/java/com/alibaba/nacos/logger/adapter/logback12/LogbackNacosLoggingAdapterBuilderTest.java new file mode 100644 index 000000000..0325883ed --- /dev/null +++ b/logger-adapter-impl/logback-adapter-12/src/test/java/com/alibaba/nacos/logger/adapter/logback12/LogbackNacosLoggingAdapterBuilderTest.java @@ -0,0 +1,34 @@ +/* + * Copyright 1999-2023 Alibaba Group Holding Ltd. + * + * Licensed 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.alibaba.nacos.logger.adapter.logback12; + +import com.alibaba.nacos.common.logging.NacosLoggingAdapter; +import org.junit.Test; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +public class LogbackNacosLoggingAdapterBuilderTest { + + @Test + public void build() { + LogbackNacosLoggingAdapterBuilder builder = new LogbackNacosLoggingAdapterBuilder(); + NacosLoggingAdapter adapter = builder.build(); + assertNotNull(adapter); + assertTrue(adapter instanceof LogbackNacosLoggingAdapter); + } +} \ No newline at end of file diff --git a/client/src/test/java/com/alibaba/nacos/client/logging/logback/LogbackNacosLoggingAdapterTest.java b/logger-adapter-impl/logback-adapter-12/src/test/java/com/alibaba/nacos/logger/adapter/logback12/LogbackNacosLoggingAdapterTest.java similarity index 93% rename from client/src/test/java/com/alibaba/nacos/client/logging/logback/LogbackNacosLoggingAdapterTest.java rename to logger-adapter-impl/logback-adapter-12/src/test/java/com/alibaba/nacos/logger/adapter/logback12/LogbackNacosLoggingAdapterTest.java index cc4aa6327..26e78daa5 100644 --- a/client/src/test/java/com/alibaba/nacos/client/logging/logback/LogbackNacosLoggingAdapterTest.java +++ b/logger-adapter-impl/logback-adapter-12/src/test/java/com/alibaba/nacos/logger/adapter/logback12/LogbackNacosLoggingAdapterTest.java @@ -1,6 +1,5 @@ /* - * - * Copyright 1999-2018 Alibaba Group Holding Ltd. + * Copyright 1999-2023 Alibaba Group Holding Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,17 +12,16 @@ * 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.alibaba.nacos.client.logging.logback; +package com.alibaba.nacos.logger.adapter.logback12; import ch.qos.logback.classic.Logger; import ch.qos.logback.classic.LoggerContext; import ch.qos.logback.classic.joran.ReconfigureOnChangeTask; import ch.qos.logback.classic.spi.LoggerContextListener; import ch.qos.logback.core.CoreConstants; -import com.alibaba.nacos.client.logging.NacosLoggingProperties; +import com.alibaba.nacos.common.logging.NacosLoggingProperties; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -35,6 +33,7 @@ import org.slf4j.LoggerFactory; import org.slf4j.impl.StaticLoggerBinder; import java.lang.reflect.Field; +import java.util.Properties; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -68,7 +67,7 @@ public class LogbackNacosLoggingAdapterTest { loggerContext.addListener(loggerContextListener); setLoggerFactory(loggerContext); } - loggingProperties = new NacosLoggingProperties("classpath:nacos-logback.xml"); + loggingProperties = new NacosLoggingProperties("classpath:nacos-logback12.xml", new Properties()); } @After @@ -125,12 +124,13 @@ public class LogbackNacosLoggingAdapterTest { @Test public void testIsAdaptedLogger() { assertTrue(logbackNacosLoggingAdapter.isAdaptedLogger(Logger.class)); - assertFalse(logbackNacosLoggingAdapter.isAdaptedLogger(org.apache.logging.log4j.core.Logger.class)); + assertFalse(logbackNacosLoggingAdapter.isAdaptedLogger(java.util.logging.Logger.class)); } @Test(expected = IllegalStateException.class) public void testLoadConfigurationFailure() { System.setProperty("nacos.logging.config", "http://localhost"); + loggingProperties = new NacosLoggingProperties("classpath:nacos-logback12.xml", System.getProperties()); logbackNacosLoggingAdapter.loadConfiguration(loggingProperties); } @@ -141,7 +141,7 @@ public class LogbackNacosLoggingAdapterTest { @Test public void testGetDefaultConfigLocation() { - assertEquals("classpath:nacos-logback.xml", logbackNacosLoggingAdapter.getDefaultConfigLocation()); + assertEquals("classpath:nacos-logback12.xml", logbackNacosLoggingAdapter.getDefaultConfigLocation()); } @Test diff --git a/client/src/test/java/com/alibaba/nacos/client/logging/logback/NacosClientPropertyActionTest.java b/logger-adapter-impl/logback-adapter-12/src/test/java/com/alibaba/nacos/logger/adapter/logback12/NacosClientPropertyActionTest.java similarity index 89% rename from client/src/test/java/com/alibaba/nacos/client/logging/logback/NacosClientPropertyActionTest.java rename to logger-adapter-impl/logback-adapter-12/src/test/java/com/alibaba/nacos/logger/adapter/logback12/NacosClientPropertyActionTest.java index 22d5c29ef..597027ca7 100644 --- a/client/src/test/java/com/alibaba/nacos/client/logging/logback/NacosClientPropertyActionTest.java +++ b/logger-adapter-impl/logback-adapter-12/src/test/java/com/alibaba/nacos/logger/adapter/logback12/NacosClientPropertyActionTest.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2022 Alibaba Group Holding Ltd. + * Copyright 1999-2023 Alibaba Group Holding Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,14 +14,14 @@ * limitations under the License. */ -package com.alibaba.nacos.client.logging.logback; +package com.alibaba.nacos.logger.adapter.logback12; import ch.qos.logback.core.ContextBase; import ch.qos.logback.core.joran.spi.ActionException; import ch.qos.logback.core.joran.spi.InterpretationContext; import ch.qos.logback.core.status.ErrorStatus; import ch.qos.logback.core.status.Status; -import com.alibaba.nacos.client.env.NacosClientProperties; +import com.alibaba.nacos.common.logging.NacosLoggingProperties; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -29,6 +29,7 @@ import org.mockito.Mockito; import org.xml.sax.Attributes; import java.util.List; +import java.util.Properties; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -37,9 +38,15 @@ public class NacosClientPropertyActionTest { ContextBase context; + private NacosLoggingProperties loggingProperties; + + private Properties properties; + @Before public void setUp() throws Exception { context = new ContextBase(); + properties = new Properties(); + loggingProperties = new NacosLoggingProperties("classpath:test.xml", properties); } @After @@ -50,7 +57,7 @@ public class NacosClientPropertyActionTest { @Test public void testLookUpVar() throws ActionException { - NacosClientProperties.PROTOTYPE.setProperty("test.nacos.logging.action.lookup", "true"); + properties.setProperty("test.nacos.logging.action.lookup", "true"); final InterpretationContext interpretationContext = new InterpretationContext(context, null); @@ -60,7 +67,7 @@ public class NacosClientPropertyActionTest { Mockito.when(mockAttr.getValue(Mockito.eq("scope"))).thenReturn("context"); Mockito.when(mockAttr.getValue(Mockito.eq("defaultValue"))).thenReturn("/root"); - NacosClientPropertyAction nacosClientPropertyAction = new NacosClientPropertyAction(); + NacosClientPropertyAction nacosClientPropertyAction = new NacosClientPropertyAction(loggingProperties); nacosClientPropertyAction.setContext(context); nacosClientPropertyAction.begin(interpretationContext, "nacosClientProperty", mockAttr); @@ -78,7 +85,7 @@ public class NacosClientPropertyActionTest { Mockito.when(mockAttr.getValue(Mockito.eq("source"))).thenReturn("test.nacos.logging.action.lookup"); Mockito.when(mockAttr.getValue(Mockito.eq("scope"))).thenReturn("context"); Mockito.when(mockAttr.getValue(Mockito.eq("defaultValue"))).thenReturn("/root"); - NacosClientPropertyAction nacosClientPropertyAction = new NacosClientPropertyAction(); + NacosClientPropertyAction nacosClientPropertyAction = new NacosClientPropertyAction(loggingProperties); nacosClientPropertyAction.setContext(context); nacosClientPropertyAction.begin(interpretationContext, "nacosClientProperty", mockAttr); List statusList = context.getStatusManager().getCopyOfStatusList(); diff --git a/client/src/test/java/com/alibaba/nacos/client/logging/logback/NacosLogbackConfiguratorAdapterV1Test.java b/logger-adapter-impl/logback-adapter-12/src/test/java/com/alibaba/nacos/logger/adapter/logback12/NacosLogbackConfiguratorAdapterV1Test.java similarity index 98% rename from client/src/test/java/com/alibaba/nacos/client/logging/logback/NacosLogbackConfiguratorAdapterV1Test.java rename to logger-adapter-impl/logback-adapter-12/src/test/java/com/alibaba/nacos/logger/adapter/logback12/NacosLogbackConfiguratorAdapterV1Test.java index cd1aed03d..ef9df73b0 100644 --- a/client/src/test/java/com/alibaba/nacos/client/logging/logback/NacosLogbackConfiguratorAdapterV1Test.java +++ b/logger-adapter-impl/logback-adapter-12/src/test/java/com/alibaba/nacos/logger/adapter/logback12/NacosLogbackConfiguratorAdapterV1Test.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.alibaba.nacos.client.logging.logback; +package com.alibaba.nacos.logger.adapter.logback12; import ch.qos.logback.core.ContextBase; import ch.qos.logback.core.joran.spi.JoranException; diff --git a/logger-adapter-impl/pom.xml b/logger-adapter-impl/pom.xml new file mode 100644 index 000000000..6f5222ad5 --- /dev/null +++ b/logger-adapter-impl/pom.xml @@ -0,0 +1,97 @@ + + + + + + nacos-all + com.alibaba.nacos + ${revision} + + 4.0.0 + + nacos-logger-adapter-impl + pom + nacos-logger-adapter-impl ${project.version} + https://nacos.io + + logback-adapter-12 + log4j2-adapter + + + + + com.alibaba.nacos + nacos-common + + + + + + release-sign-artifacts + + + + maven-shade-plugin + + + maven-jar-plugin + + + pure-jar + package + + jar + + + pure + + + + + + + + + release-nacos + + + + maven-shade-plugin + + + maven-jar-plugin + + + pure-jar + package + + jar + + + pure + + + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 9066f7319..0fe10389a 100644 --- a/pom.xml +++ b/pom.xml @@ -616,6 +616,7 @@ plugin-default-impl prometheus persistence + logger-adapter-impl