提交 69386baa 编写于 作者: X xiongchun

优化了动态日志级别切换组件

上级 8b655f92
......@@ -16,7 +16,7 @@
#
spring.application.name=pangu-examples-config-local
demo.app.id=1000001
demo.app.id=10000001
logging.level.root=INFO
logging.level.com.gitee.pulanos.pangu=INFO
\ No newline at end of file
......@@ -34,7 +34,6 @@ import javax.annotation.PostConstruct;
*/
@Slf4j
@SpringBootApplication
@ComponentScan({"com.gitee.pulanos.pangu"})
public class NacosConfigurationApplication {
public static void main(String[] args) {
......
......@@ -19,8 +19,6 @@ spring.profiles.active=${spring.profiles.active:dev}
nacos.config.bootstrap.enable=true
nacos.config.bootstrap.log-enable=true
nacos.config.auto-refresh=true
#同名的远程配置将覆盖本地配置
nacos.config.remote-first=true
#对应Nacos配置中心的命名空间ID
nacos.config.namespace=${nacos.namespace:pangu-dev}
nacos.config.server-addr=${nacos.server-addr:127.0.0.1:8848}
......
......@@ -80,6 +80,10 @@ public final class Constants {
* 应用退出
*/
public final static String APP_EXIT = "(ô‿ô) PanGu Dev Framework Stopped ㊥ 盘古开发框架已停止";
/**
* OK消息
*/
public final static String OK = "[OK] ";
}
/**
......
......@@ -21,9 +21,6 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;
import javax.annotation.PreDestroy;
/**
* 创建 {@link SpringApplication} 和 {@link ConfigurableApplicationContext} 实例。
......
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.gitee.pulanos.pangu.framework.autoconfigure;
import cn.hutool.core.util.StrUtil;
import com.alibaba.nacos.api.config.ConfigType;
import com.alibaba.nacos.api.config.annotation.NacosConfigListener;
import com.alibaba.nacos.spring.util.ConfigParseUtils;
import com.gitee.pulanos.pangu.framework.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.logging.LogLevel;
import org.springframework.boot.logging.LoggingSystem;
import java.util.Properties;
/**
* DynamicLogSwitcher
*
* @author xiongchun
* @date 2021-02-10
*/
@Slf4j
public class DynamicLogSwitcher {
@Autowired
private LoggingSystem loggingSystem;
private static final String LOGGER_PREFIX = "logging.level.";
@NacosConfigListener(dataId = "${nacos.config.data-id}", timeout = 5000)
public void onChange(String newCfgText) throws Exception{
Properties properties = ConfigParseUtils.toProperties(newCfgText, ConfigType.PROPERTIES.getType());
for (Object key : properties.keySet()) {
String logKey = String.valueOf(key);
if (logKey.startsWith(LOGGER_PREFIX)) {
String logValue = properties.getProperty(logKey, "INFO");
refreshLogLevelByKey(logKey, logValue);
}
}
}
public void refreshLogLevelByKey(String logKey, String logValue) {
if (StrUtil.isNotEmpty(logKey) && StrUtil.isNotEmpty(logValue)) {
logValue = logValue.toUpperCase();
LogLevel logLevel = LogLevel.valueOf(logValue);
loggingSystem.setLogLevel(StrUtil.subAfter(logKey, LOGGER_PREFIX, true), logLevel);
log.info(StrUtil.format("{}成功热刷新了日志级别 >> {}:{}", Constants.Msg.OK, logKey, logValue));
}
}
}
......@@ -17,22 +17,40 @@
package com.gitee.pulanos.pangu.framework.autoconfigure;
import cn.hutool.core.util.StrUtil;
import com.alibaba.nacos.api.config.ConfigService;
import com.gitee.pulanos.pangu.framework.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* ApplicationExitHookConfiguration
* PanguBaseAutoConfiguration
*
* @author xiongchun
*/
@Slf4j
@Configuration
@ConditionalOnMissingBean(ApplicationExitHook.class)
public class ApplicationExitHookConfiguration {
public class PanguBaseAutoConfiguration {
@Bean
@ConditionalOnMissingBean(ApplicationExitHook.class)
public ApplicationExitHook createApplicationExitHook(){
return new ApplicationExitHook();
ApplicationExitHook applicationExitHook = new ApplicationExitHook();
log.info("{}{}{}", Constants.Msg.OK, "实例化并自动装配了Bean组件:", StrUtil.lowerFirst(ApplicationExitHook.class.getSimpleName()));
return applicationExitHook;
}
@Bean
@ConditionalOnProperty(prefix = "nacos", name = "config.data-id")
@ConditionalOnClass(ConfigService.class)
public DynamicLogSwitcher createDynamicLogSwitcher() {
DynamicLogSwitcher dynamicLogSwitcher = new DynamicLogSwitcher();
log.info("{}{}{}", Constants.Msg.OK, "实例化并自动装配了Bean组件:", StrUtil.lowerFirst(DynamicLogSwitcher.class.getSimpleName()));
return dynamicLogSwitcher;
}
}
......@@ -16,4 +16,4 @@
#
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.gitee.pulanos.pangu.framework.autoconfigure.ApplicationExitHookConfiguration
com.gitee.pulanos.pangu.framework.autoconfigure.PanguBaseAutoConfiguration
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册