Skip to content

Commit

Permalink
[ISSUE alibaba#9860] abstract the nacos logback configurator to solve…
Browse files Browse the repository at this point in the history
… the problem between different version (alibaba#9934)

* logback update

* update comment

* ident update

* comment update

* judge version

* remove logback-adapter
  • Loading branch information
hujun-w-2 authored Feb 22, 2023
1 parent 4242fc6 commit 579571c
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@
import ch.qos.logback.classic.spi.LoggerContextListener;
import ch.qos.logback.core.CoreConstants;
import com.alibaba.nacos.client.logging.AbstractNacosLogging;
import com.alibaba.nacos.common.log.NacosLogbackConfigurator;
import com.alibaba.nacos.common.spi.NacosServiceLoader;
import com.alibaba.nacos.common.utils.ResourceUtils;
import org.slf4j.impl.StaticLoggerBinder;
import org.slf4j.LoggerFactory;
import java.util.Collection;
import java.util.stream.Collectors;

/**
* Support for Logback version 1.0.8 or higher
Expand All @@ -37,6 +41,19 @@ public class LogbackNacosLogging extends AbstractNacosLogging {

private static final String NACOS_LOGBACK_LOCATION = "classpath:nacos-logback.xml";

private Integer userVersion = 2;

/**
* logback use 'ch.qos.logback.core.model.Model' since 1.3.0, set logback version during initialization.
*/
public LogbackNacosLogging() {
try {
Class.forName("ch.qos.logback.core.model.Model");
} catch (ClassNotFoundException e) {
userVersion = 1;
}
}

@Override
public void loadConfiguration() {
LoggerContext loggerContext = loadConfigurationOnStart();
Expand All @@ -57,10 +74,13 @@ private boolean hasListener(LoggerContext loggerContext) {
private LoggerContext loadConfigurationOnStart() {
String location = getLocation(NACOS_LOGBACK_LOCATION);
try {
LoggerContext loggerContext = (LoggerContext) StaticLoggerBinder.getSingleton().getLoggerFactory();
NacosJoranConfigurator configurator = new NacosJoranConfigurator();
configurator.setContext(loggerContext);
configurator.doNacosConfigure(ResourceUtils.getResourceUrl(location));
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
Collection<NacosLogbackConfigurator> nacosLogbackConfigurators = NacosServiceLoader.load(
NacosLogbackConfigurator.class);
NacosLogbackConfigurator nacosLogbackConfigurator = nacosLogbackConfigurators.stream()
.filter(c -> c.getVersion() == userVersion).collect(Collectors.toList()).get(0);
nacosLogbackConfigurator.setContext(loggerContext);
nacosLogbackConfigurator.configure(ResourceUtils.getResourceUrl(location));
return loggerContext;
} catch (Exception e) {
throw new IllegalStateException("Could not initialize Logback Nacos logging from " + location, e);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 1999-2018 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.client.logging.logback;

import com.alibaba.nacos.client.env.NacosClientProperties;
import com.alibaba.nacos.common.log.NacosLogbackProperties;

/**
* adapter to higher version of logback (>= 1.4.5).
*
* @author hujun
*/
public class NacosClientLogbackProperties implements NacosLogbackProperties {

@Override
public String getValue(String source, String defaultValue) {
return NacosClientProperties.PROTOTYPE.getProperty(source, defaultValue);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
package com.alibaba.nacos.client.logging.logback;

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.log.NacosLogbackConfigurator;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
Expand All @@ -34,7 +35,7 @@
* @author <a href="mailto:hujun3@xiaomi.com">hujun</a>
* @see <a href="https://github.com/alibaba/nacos/issues/6999">#6999</a>
*/
public class NacosJoranConfigurator extends JoranConfigurator {
public class NacosLogbackConfiguratorAdapterV1 extends JoranConfigurator implements NacosLogbackConfigurator {

/**
* ensure that Nacos configuration does not affect user configuration savepoints.
Expand All @@ -51,13 +52,24 @@ public void addInstanceRules(RuleStore rs) {
rs.addRule(new ElementSelector("configuration/nacosClientProperty"), new NacosClientPropertyAction());
}

@Override
public int getVersion() {
return 1;
}

@Override
public void setContext(Object loggerContext) {
super.setContext((Context) loggerContext);
}

/**
* ensure that Nacos configuration does not affect user configuration scanning url.
*
* @param url config url
* @throws JoranException e
* @throws Exception e
*/
public void doNacosConfigure(URL url) throws JoranException {
@Override
public void configure(URL url) throws Exception {
InputStream in = null;
try {
URLConnection urlConnection = url.openConnection();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# Copyright 1999-2021 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.client.logging.logback.NacosLogbackConfiguratorAdapterV1
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# Copyright 1999-2021 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.client.logging.logback.NacosClientLogbackProperties
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 1999-2022 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.log;

import java.net.URL;

/**
* logback configurator interface,different version can adapter this.
* @author hujun
*/
public interface NacosLogbackConfigurator {

/**
* config logback.
* @param resourceUrl resourceUrl
* @throws Exception exception
*/
void configure(URL resourceUrl) throws Exception;

/**
* logback configurator will be sorted by version.
* @return version
*/
int getVersion();

/**
* set loggerContext.
* @param loggerContext loggerContext
*/
void setContext(Object loggerContext);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 1999-2022 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.log;

/**
* nacos logback properties.
* @author hujun
*/
public interface NacosLogbackProperties {
/**
* get value.
*
* @param source source
* @param defaultValue defaultValue
* @return value
*/
String getValue(String source, String defaultValue);
}

0 comments on commit 579571c

Please sign in to comment.