In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "how to understand the integrated apollo dynamic log". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Preface
Dynamic adjustment of online log levels is a very common scenario, and it is very easy to implement with the help of a configuration central component like apollo. As the official technical support of apollo, bloggers often see users in the technical group asking whether apollo can host the configuration files of logback. After all, with the configuration center, it is our ultimate goal to eliminate all local configurations and hand them over to apollo management. However, apollo does not have the ability to directly host logback-spring.xml configuration files, but we can completely disable the logback-spring.xml configuration and use the configuration driver in apollo based on the loading mechanism of spring and logback. Moreover, after the transformation, the flexibility and expansibility of the log system are greatly improved.
Apollo dynamic log
What is the apollo dynamic log? It may be ambiguous to say this directly, thinking that it is a log in apollo, but it is not. To take a simple example, for example, log.debug () is used to print logs in many parts of our project to facilitate troubleshooting through log information, but in general, the log level of the production environment is configured to info. The debug level log is opened temporarily only when there is a need to troubleshoot online problems. At this point, you can only change the configuration file, adjust the log level to debug, and then repackage the deployment verification. The process is not only tedious and time-consuming, but also destroys the "environment of the crime scene" at that time, resulting in inaccurate judgment. If the application has the ability of apollo dynamic logging, you can hot update the log level and print the debug level log immediately by modifying the configuration of apollo and then submitting it. This is called the apollo dynamic log. To achieve this effect, you need to have two capabilities, provided by spring and apollo.
Spring log system hot update log level
In spring applications, spring adapts to mainstream logging frameworks such as logback and log4j2. On top of these logging frameworks, we abstract our own log system service. Here, we use spring's LoggingSystem to hot update the log level. This class is added to the spring container when the log system is initialized, so as long as it is within the context management scope of spring, it can be directly injected. The following is the description of api mainly used:
/ * sets the log level for a given logger. * @ param loggerName the name of the logger to set ({@ code null} can be used for the root logger). * @ param level log level * / public void setLogLevel (String loggerName, LogLevel level) {throw new UnsupportedOperationException ("Unable to set log level");} apollo log configuration changes are dynamically issued
Apollo as a distributed configuration center, centralized configuration management and configuration hot update are its core functions. In addition, apollo also provides the function of sending and listening for configuration changes. Based on the design of this configuration listening, it is very easy to implement dynamic logging. And not only can the log dynamic hot more, based on this idea, connection pool, data sources and so on can be easily realized. There are several ways for apollo to listen for configuration changes, which can be manually added through Config instances, such as:
@ ApolloConfig public Config config; public void addConfigChangeListener () {config.addChangeListener (changeEvent- > {System.out.println ("config change keys" + changeEvent.changedKeys ();});}
It can also be directly driven by annotations.
@ ApolloConfigChangeListener public void addConfigChangeListener (ConfigChangeEvent changeEvent) {System.out.println ("config change keys" + changeEvent.changedKeys ());} realize hot update of log adjustment
With the above capabilities, the log loading configuration supported by spring, such as:
Logging.level.org.springframework.web=debug
Logging.level.org.hibernate=error
You can complete the following code. When you need to adjust the log level, modify the configuration in apollo to take effect in real time.
@ Configurationpublic class LogbackConfiguration {private static final Logger logger = LoggerFactory.getLogger (LoggerConfiguration.class); private static final String LOGGER_TAG = "logging.level."; private final LoggingSystem loggingSystem; public LogbackConfiguration (LoggingSystem loggingSystem) {this.loggingSystem = loggingSystem } @ ApolloConfigChangeListener private void onChange (ConfigChangeEvent changeEvent) {for (String key: changeEvent.changedKeys ()) {if (this.containsIgnoreCase (key, LOGGER_TAG)) {String strLevel = changeEvent.getChange (key). GetNewValue (); LogLevel level = LogLevel.valueOf (strLevel.toUpperCase ()); loggingSystem.setLogLevel (key.replace (LOGGER_TAG, "), level) Logger.info ("logging changed: {}, oldValue: {}, newValue: {}", key, changeEvent.getChange (key). GetOldValue (), strLevel);}} private 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
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.