In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces springboot how to dynamically adjust the log level, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
1.springboot uses log4j2
Springboot uses common-logging, which is compatible with various logging frameworks such as log4j2,slf4,logback and so on. By default, logback is used at the bottom. We can remove the dependency of logback and introduce starter of log4j2.
As follows:
two。 Specify log profile and log level
(this configuration is not limited to log4j2, but also applies to other logging frameworks)
Add the xml configuration file of log4j2 in the resources directory, and the default spring-boot will load the log configuration file named log4j2.xml, or log4j2-file.xml under classpath.
You can also specify the log configuration file to be loaded in the configuration file of spring, and dynamically adjust the log level of each directory.
Logging: config: classpath:log4j2.xml level: com.ly: debug org.springframework: info
This parameter can override the configuration items in the jar with system parameters or startup parameters.
Java-jar-Dlogging.config= "xxx" test.jarjava-jar test.jar-logging.config= "xxx" 3. Dynamically adjust the log level through springboot-actuator
(suitable for production environment)
Spring-boot-actuator is a monitoring tool of springboot, it can expose some endPoint in the way of http or JMX, and the built-in endpoint has health,info,beans,loggers and so on.
We can adjust the log level dynamically through loggers without restarting the service.
If you want to use webEndPoint, the project must include web-starter-related dependencies, because actuator's httpEndPoint is integrated as mvc.
3.1 introduce endPoint endpoints that rely on org.springframework.boot spring-boot-starter-actuator 3.2 to enable loggers in the configuration file management: endpoints: web: exposure: include: loggers3.3 to initiate a http request to change the log level in the pom file
URI defaults to / actuator+endpoint+ package name
$curl 'http://localhost:8080/actuator/loggers/com.example'-I-X POST\-H' Content-Type: application/json'\-d'{"configuredLevel": "debug"}'
The principle can be seen from the implementation of LoggersEndPoint.
Principle of 4.spring boot log initialization
There is a loggingApplicationListener listener that listens to spring events, reads the log configuration in the spring container, and initializes the log.
Thank you for reading this article carefully. I hope the article "how to dynamically adjust the log level of springboot" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
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.