feature: support Polaris configuration center extension plugin interface and support dynamic modification of log levels. (#1095)
Co-authored-by: melodyl <lovemycode@qq.com>pull/1110/head
parent
f68008e068
commit
e8730cb07d
@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* 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.tencent.cloud.polaris.config.adapter;
|
||||
|
||||
import com.tencent.polaris.configuration.api.core.ConfigFileService;
|
||||
|
||||
import org.springframework.core.env.CompositePropertySource;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
|
||||
/**
|
||||
* @Date Jul 23, 2023 2:57:49 PM
|
||||
* @author juanyinyang
|
||||
*/
|
||||
public interface PolarisConfigCustomExtensionLayer {
|
||||
void initRegisterConfig(PolarisConfigPropertyAutoRefresher polarisConfigPropertyAutoRefresher);
|
||||
|
||||
void initConfigFiles(Environment environment, CompositePropertySource compositePropertySource, PolarisPropertySourceManager polarisPropertySourceManager, ConfigFileService configFileService);
|
||||
|
||||
void executeAfterLocateConfigReturning(CompositePropertySource compositePropertySource);
|
||||
boolean executeRegisterPublishChangeListener(PolarisPropertySource polarisPropertySource);
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* 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.tencent.cloud.polaris.config.adapter;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.ServiceLoader;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* @author juanyinyang
|
||||
* @Date 2023年8月10日 下午4:11:05
|
||||
*/
|
||||
public final class PolarisServiceLoaderUtil {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(PolarisServiceLoaderUtil.class);
|
||||
private PolarisServiceLoaderUtil() {
|
||||
}
|
||||
// this class provides customized logic for some customers to configure special business group files
|
||||
private static PolarisConfigCustomExtensionLayer polarisConfigCustomExtensionLayer;
|
||||
static {
|
||||
ServiceLoader<PolarisConfigCustomExtensionLayer> polarisConfigCustomExtensionLayerLoader = ServiceLoader.load(PolarisConfigCustomExtensionLayer.class);
|
||||
Iterator<PolarisConfigCustomExtensionLayer> polarisConfigCustomExtensionLayerIterator = polarisConfigCustomExtensionLayerLoader.iterator();
|
||||
// Generally, there is only one implementation class. If there are multiple, the last one is loaded
|
||||
while (polarisConfigCustomExtensionLayerIterator.hasNext()) {
|
||||
polarisConfigCustomExtensionLayer = polarisConfigCustomExtensionLayerIterator.next();
|
||||
LOGGER.info("[SCT Config] PolarisConfigFileLocator init polarisConfigCustomExtensionLayer:{}", polarisConfigCustomExtensionLayer);
|
||||
}
|
||||
}
|
||||
|
||||
public static PolarisConfigCustomExtensionLayer getPolarisConfigCustomExtensionLayer() {
|
||||
return polarisConfigCustomExtensionLayer;
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* 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.tencent.cloud.polaris.config.listener;
|
||||
|
||||
public interface SyncConfigChangeListener extends ConfigChangeListener {
|
||||
default boolean isAsync() {
|
||||
return true;
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* 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.tencent.cloud.polaris.config.logger;
|
||||
|
||||
/**
|
||||
* @author juanyinyang
|
||||
*/
|
||||
public enum Level {
|
||||
|
||||
/** log level. */
|
||||
TRACE("TRACE"), DEBUG("DEBUG"), INFO("INFO"), WARN("WARN"), ERROR("ERROR"), FATAL("FATAL"), OFF("OFF");
|
||||
|
||||
private String level;
|
||||
|
||||
Level(String level) {
|
||||
this.level = level;
|
||||
}
|
||||
public String getLevel() {
|
||||
return level;
|
||||
}
|
||||
|
||||
public static Level levelOf(String level) {
|
||||
for (Level l : Level.values()) {
|
||||
if (l.level.equalsIgnoreCase(level)) {
|
||||
return l;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* 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.tencent.cloud.polaris.config.logger;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import org.springframework.boot.context.event.ApplicationStartedEvent;
|
||||
import org.springframework.boot.logging.LoggingSystem;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
|
||||
/**
|
||||
* @author juanyinyang
|
||||
*/
|
||||
public class PolarisConfigLoggerApplicationListener implements ApplicationListener<ApplicationEvent> {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(PolarisConfigLoggerApplicationListener.class);
|
||||
/**
|
||||
* @see org.springframework.context.ApplicationListener#onApplicationEvent(org.springframework.context.ApplicationEvent)
|
||||
*/
|
||||
@Override
|
||||
public void onApplicationEvent(ApplicationEvent event) {
|
||||
try {
|
||||
// Initialize application loggingSystem.
|
||||
if (event instanceof ApplicationStartedEvent) {
|
||||
ApplicationStartedEvent startedEvent = (ApplicationStartedEvent) event;
|
||||
ClassLoader classLoader = startedEvent.getSpringApplication().getClassLoader();
|
||||
LoggingSystem loggingSystem = LoggingSystem.get(classLoader);
|
||||
LOGGER.info("PolarisConfigLoggerApplicationListener onApplicationEvent init loggingSystem:{}", loggingSystem);
|
||||
PolarisConfigLoggerContext.setLogSystem(loggingSystem);
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
LOGGER.error("PolarisConfigLoggerApplicationListener onApplicationEvent exception:", e);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,106 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* 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.tencent.cloud.polaris.config.logger;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import org.springframework.boot.logging.LogLevel;
|
||||
import org.springframework.boot.logging.LoggingSystem;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import static org.springframework.boot.logging.LoggingSystem.ROOT_LOGGER_NAME;
|
||||
|
||||
/**
|
||||
* @author juanyinyang
|
||||
*/
|
||||
public final class PolarisConfigLoggerContext {
|
||||
|
||||
private static LoggingSystem loggingSystem;
|
||||
|
||||
private PolarisConfigLoggerContext() {
|
||||
|
||||
}
|
||||
protected static void setLogSystem(LoggingSystem logSystem) {
|
||||
Assert.notNull(logSystem, "Logging System should not be null");
|
||||
PolarisConfigLoggerContext.loggingSystem = logSystem;
|
||||
}
|
||||
|
||||
public static void setLevel(String loggerName, String level) {
|
||||
if (loggingSystem == null) {
|
||||
printLog("[SCT Config] PolarisConfigLoggerContext logger: [" + loggerName + "] change to target level fail. caused by internal exception:" + level, Level.WARN);
|
||||
return;
|
||||
}
|
||||
Level loggerLevel = Level.levelOf(level);
|
||||
if (loggerLevel == null) {
|
||||
printLog("[SCT Config] PolarisConfigLoggerContext logger: [" + loggerName + "] change to target level fail. caused by level is not support, level:" + level, Level.WARN);
|
||||
return;
|
||||
}
|
||||
LogLevel logLevel = null;
|
||||
switch (loggerLevel) {
|
||||
case TRACE:
|
||||
logLevel = LogLevel.TRACE;
|
||||
break;
|
||||
case DEBUG:
|
||||
logLevel = LogLevel.DEBUG;
|
||||
break;
|
||||
case OFF:
|
||||
logLevel = LogLevel.OFF;
|
||||
break;
|
||||
case INFO:
|
||||
logLevel = LogLevel.INFO;
|
||||
break;
|
||||
case WARN:
|
||||
logLevel = LogLevel.WARN;
|
||||
break;
|
||||
case ERROR:
|
||||
logLevel = LogLevel.ERROR;
|
||||
break;
|
||||
case FATAL:
|
||||
logLevel = LogLevel.FATAL;
|
||||
break;
|
||||
default:
|
||||
printLog("[SCT Config] PolarisConfigLoggerContext logger: [" + loggerName + "] setLevel fail. caused by level is not support, level: " + level, Level.WARN);
|
||||
}
|
||||
loggingSystem.setLogLevel(loggerName, logLevel);
|
||||
printLog("[SCT Config] PolarisConfigLoggerContext logger: [" + loggerName + "] changed to level:" + level, Level.INFO);
|
||||
}
|
||||
/**
|
||||
* print log.
|
||||
*/
|
||||
private static void printLog(String message, Level level) {
|
||||
Logger logger = LoggerFactory.getLogger(ROOT_LOGGER_NAME);
|
||||
if (level.ordinal() <= Level.INFO.ordinal()) {
|
||||
if (logger != null) {
|
||||
logger.info(message);
|
||||
}
|
||||
else {
|
||||
StdLog.info(message);
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (logger != null) {
|
||||
logger.warn(message);
|
||||
}
|
||||
else {
|
||||
StdLog.warn(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,96 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* 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.tencent.cloud.polaris.config.logger;
|
||||
|
||||
import java.io.PrintStream;
|
||||
import java.util.Calendar;
|
||||
|
||||
/**
|
||||
* internal output logger.
|
||||
* @author juanyinyang
|
||||
*/
|
||||
public final class StdLog {
|
||||
|
||||
private static final String CLASS_INFO = StdLog.class.getName();
|
||||
|
||||
|
||||
protected static boolean debugEnabled = false;
|
||||
|
||||
/**
|
||||
* *enable info level log.
|
||||
*/
|
||||
protected static boolean infoEnabled = true;
|
||||
|
||||
/**
|
||||
* quite mode will out put nothing.
|
||||
*/
|
||||
protected static boolean quietMode = false;
|
||||
|
||||
private static final String DEBUG_FIX = "StdLog:DEBUG: ";
|
||||
|
||||
private static final String INFO_FIX = "StdLog:INFO: ";
|
||||
|
||||
private static final String WARN_FIX = "StdLog:WARN: ";
|
||||
|
||||
private StdLog() {
|
||||
}
|
||||
|
||||
public static void setQuietMode(boolean quietMode) {
|
||||
StdLog.quietMode = quietMode;
|
||||
}
|
||||
|
||||
public static void setInfoEnabled(boolean infoEnabled) {
|
||||
StdLog.infoEnabled = infoEnabled;
|
||||
}
|
||||
|
||||
public static void setDebugEnabled(boolean debugEnabled) {
|
||||
StdLog.debugEnabled = debugEnabled;
|
||||
}
|
||||
|
||||
public static void debug(String msg) {
|
||||
if (debugEnabled && !quietMode) {
|
||||
println(System.out, DEBUG_FIX + msg);
|
||||
}
|
||||
}
|
||||
|
||||
public static void info(String msg) {
|
||||
if (infoEnabled && !quietMode) {
|
||||
println(System.out, INFO_FIX + msg);
|
||||
}
|
||||
}
|
||||
|
||||
public static void warn(String msg) {
|
||||
if (infoEnabled && !quietMode) {
|
||||
println(System.err, WARN_FIX + msg);
|
||||
}
|
||||
}
|
||||
|
||||
public static void warn(String msg, Throwable t) {
|
||||
if (quietMode) {
|
||||
return;
|
||||
}
|
||||
println(System.err, WARN_FIX + msg);
|
||||
if (t != null) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static void println(PrintStream out, String msg) {
|
||||
out.println(Calendar.getInstance().getTime().toString() + " " + CLASS_INFO + " " + msg);
|
||||
}
|
||||
}
|
Loading…
Reference in new issue