Apollo配置中心動態刷新日誌級別
阿新 • • 發佈:2019-01-29
vat res sea set ica key conf 日誌級別 arch
Apollo配置中心動態刷新日誌級別
- 添加次配置後,當在apollo上面調整日誌級別不需要重啟服務器,馬上就能生效
/** * 結合apollo動態刷新日誌級別 * @author: nj * @date: 2019/1/21:下午5:00 */ @Configuration public class LogListenerConfig { private static final Logger logger = LoggerFactory.getLogger(LoggerConfiguration.class); /** * 監聽關鍵字,當配置中心的依次開頭的配置發生變化時,日誌級別刷新 */ private static final String LOGGER_TAG = "loggers.root."; @Autowired private LoggingSystem loggingSystem; /** * 可以指定具體的namespace,未指定時使用的是 application這個namespace */ @ApolloConfig private Config config; @ApolloConfigChangeListener private void onChange(ConfigChangeEvent changeEvent) { refreshLoggingLevels(); } @PostConstruct private void refreshLoggingLevels() { Set<String> keyNames = config.getPropertyNames(); for (String key : keyNames) { if (containsIgnoreCase(key, LOGGER_TAG)) { String strLevel = config.getProperty(key, "info"); LogLevel level = LogLevel.valueOf(strLevel.toUpperCase()); //重置日誌級別,馬上生效 //loggingSystem.setLogLevel(key.replace(LOGGER_TAG, ""), level); loggingSystem.setLogLevel("", level); logger.info("{}:{}", key, strLevel); } } } private static boolean containsIgnoreCase(String str, String searchStr) { if (str == null || searchStr == null) { return false; } int len = searchStr.length(); int max = str.length() - len; for (int i = 0; i <= max; i++) { if (str.regionMatches(true, i, searchStr, 0, len)) { return true; } } return false; } }
Apollo配置中心動態刷新日誌級別