* Move some basic logging classes into common module. * Move logback 12 version adapter to single module. * For checkstyle. * Enhance logger adapter pom. * Refactor the adapter initialization logic to avoid spi loader error. * Move log4j2 logger adapter to single module. * Fix the ut cover and create useless dir.
This commit is contained in:
parent
720519b1dc
commit
4d30b042ee
2
.github/workflows/ci.yml
vendored
2
.github/workflows/ci.yml
vendored
@ -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
|
||||
|
@ -41,23 +41,10 @@
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-core</artifactId>
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<artifactId>nacos-api</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-api</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-slf4j-impl</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<artifactId>nacos-common</artifactId>
|
||||
@ -75,19 +62,32 @@
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<artifactId>nacos-api</artifactId>
|
||||
<optional>true</optional>
|
||||
<groupId>com.alibaba.nacos</groupId>
|
||||
<artifactId>nacos-logback-adapter-12</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
<groupId>com.alibaba.nacos</groupId>
|
||||
<artifactId>nacos-log4j2-adapter</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-core</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-core</artifactId>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-api</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-slf4j-impl</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
|
@ -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<? extends Logger> 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<? extends Logger> loggerClass) {
|
||||
return null != loggingAdapter && loggingAdapter.isEnabled() && loggingAdapter.isAdaptedLogger(loggerClass);
|
||||
}
|
||||
|
||||
private void scheduleReloadTask() {
|
||||
ScheduledExecutorService reloadContextService = ExecutorFactory.Managed
|
||||
.newSingleScheduledExecutorService("Nacos-Client",
|
||||
|
@ -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);
|
||||
|
@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.nacos.client.logging;
|
||||
package com.alibaba.nacos.common.logging;
|
||||
|
||||
/**
|
||||
* Nacos client logging adapter.
|
@ -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}.
|
||||
*
|
||||
* <p>
|
||||
* Why not directly SPI {@link NacosLoggingAdapter}?
|
||||
* </p>
|
||||
* <p>
|
||||
* To avoid some {@link NacosLoggingAdapter} initialization error casue the SPI loader exit.
|
||||
* </p>
|
||||
*
|
||||
* @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();
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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"));
|
||||
}
|
||||
}
|
@ -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()) {
|
||||
|
55
logger-adapter-impl/log4j2-adapter/pom.xml
Normal file
55
logger-adapter-impl/log4j2-adapter/pom.xml
Normal file
@ -0,0 +1,55 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
~ 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.
|
||||
-->
|
||||
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>nacos-logger-adapter-impl</artifactId>
|
||||
<groupId>com.alibaba.nacos</groupId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>nacos-log4j2-adapter</artifactId>
|
||||
<name>nacos-log4j2-adapter ${revision}</name>
|
||||
<description>Nacos client log4j2 adapter pom.xml file</description>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-core</artifactId>
|
||||
<scope>provided</scope>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-api</artifactId>
|
||||
<scope>provided</scope>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-slf4j-impl</artifactId>
|
||||
<scope>provided</scope>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -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);
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
@ -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.
|
||||
*
|
||||
* <p>
|
||||
* 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}.
|
||||
* </p>
|
||||
*
|
||||
* @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);
|
||||
}
|
||||
|
||||
}
|
@ -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,18 +14,17 @@
|
||||
* 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:
|
||||
* <SizeBasedTriggeringPolicy size="${nacosClientProperty:JM.LOG.FILE.SIZE:-10MB}"/>
|
||||
*
|
||||
* @author onewe
|
||||
*/
|
||||
@Plugin(name = "nacosClientProperty", category = StrLookup.CATEGORY)
|
||||
@ -33,6 +32,6 @@ public class NacosClientPropertiesLookup extends AbstractLookup {
|
||||
|
||||
@Override
|
||||
public String lookup(LogEvent event, String key) {
|
||||
return NacosClientProperties.PROTOTYPE.getProperty(key);
|
||||
return Log4j2NacosLoggingPropertiesHolder.getValue(key);
|
||||
}
|
||||
}
|
@ -14,5 +14,4 @@
|
||||
# limitations under the License.
|
||||
#
|
||||
|
||||
com.alibaba.nacos.client.logging.logback.LogbackNacosLoggingAdapter
|
||||
com.alibaba.nacos.client.logging.log4j2.Log4J2NacosLoggingAdapter
|
||||
com.alibaba.nacos.logger.adapter.log4j2.Log4j2NacosLoggingAdapterBuilder
|
@ -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());
|
@ -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);
|
||||
}
|
||||
}
|
@ -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);
|
58
logger-adapter-impl/logback-adapter-12/pom.xml
Normal file
58
logger-adapter-impl/logback-adapter-12/pom.xml
Normal file
@ -0,0 +1,58 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
~ 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.
|
||||
-->
|
||||
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>nacos-logger-adapter-impl</artifactId>
|
||||
<groupId>com.alibaba.nacos</groupId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>nacos-logback-adapter-12</artifactId>
|
||||
<name>nacos-logback-adapter-12 ${revision}</name>
|
||||
<description>Nacos client logback adapter for logback 1.2.x and lower version pom.xml file</description>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<version>1.7.26</version>
|
||||
<scope>provided</scope>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
<version>1.2.13</version>
|
||||
<scope>provided</scope>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-core</artifactId>
|
||||
<version>1.2.13</version>
|
||||
<scope>provided</scope>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -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);
|
@ -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();
|
||||
}
|
||||
}
|
@ -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:
|
||||
* <nacosClientProperty scope="context" name="logPath" source="system.log.path" defaultValue="/root" />
|
||||
*
|
||||
* @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);
|
||||
}
|
||||
}
|
@ -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 <a href="mailto:hujun3@xiaomi.com">hujun</a>
|
||||
* @see <a href="https://github.com/alibaba/nacos/issues/6999">#6999</a>
|
||||
*/
|
||||
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 {
|
@ -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
|
@ -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
|
@ -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);
|
||||
}
|
||||
}
|
@ -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
|
@ -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<Status> statusList = context.getStatusManager().getCopyOfStatusList();
|
@ -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;
|
97
logger-adapter-impl/pom.xml
Normal file
97
logger-adapter-impl/pom.xml
Normal file
@ -0,0 +1,97 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
~ 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.
|
||||
-->
|
||||
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>nacos-all</artifactId>
|
||||
<groupId>com.alibaba.nacos</groupId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>nacos-logger-adapter-impl</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<name>nacos-logger-adapter-impl ${project.version}</name>
|
||||
<url>https://nacos.io</url>
|
||||
<modules>
|
||||
<module>logback-adapter-12</module>
|
||||
<module>log4j2-adapter</module>
|
||||
</modules>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.nacos</groupId>
|
||||
<artifactId>nacos-common</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>release-sign-artifacts</id>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>pure-jar</id>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>jar</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<classifier>pure</classifier>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
<profile>
|
||||
<id>release-nacos</id>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>pure-jar</id>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>jar</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<classifier>pure</classifier>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
</profiles>
|
||||
|
||||
</project>
|
Loading…
Reference in New Issue
Block a user