Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the operation of SpringBoot to dynamically modify the log level

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the SpringBoot dynamic modification log level operation is what the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, with a certain reference value, I believe that everyone after reading this SpringBoot dynamic modification log level operation is what the article will have a harvest, let's take a look.

The traditional practice is generally:

1. Modify the log level in the configuration

2. Restart the application

3. Check the error log to troubleshoot the problem

This process requires restarting the application, which is troublesome and inefficient, and it is impossible to stop and restart casually for large online projects. Is there a way to dynamically modify the log level without restarting the application?

Next, let Lao Wan teach you how to dynamically modify the log level through the actuator component of SpringBoot.

1. Add dependent org.springframework.boot spring-boot-starter-actuator 2, configure actuator exposed port # enable actuator port management.endpoints.enabled-by-default=fasle# set the access root path of actuator, default is / actuatormanagement.endpoints.web.base-path=/message# enabled endpoint management.endpoints.web.exposure.include=loggers

Here I changed the default access path of actuator / actuator to / message so that it is consistent with the underlying access path of the project.

The configuration method in 2 of enabling ports:

Method 1: (recommended)

Management.endpoints.web.exposure.include=loggers

Method 2: (the test in this way does not work)

Management.endpoint.loggers.enabled=true

Supplement: how to disable the info port

Management.endpoints.enabled-by-default=falsemanagement.endpoint.info.enabled=true

About the actuator component is known as one of the four major components of spring boot, powerful, everyone on the Internet to find some information to further understand.

Actuator endpoint port description: ID description enables auditevents to display audit event information for the current application by default Yesbeans displays a complete list of all Spring Beans in an application Yesconditions displays the status of the configuration class and the automatic configuration class (configuration and auto-configuration classes) and why they are applied or not Yesconfigprops displays a collection list of all @ ConfigurationProperties Yesenv displays the properties Yesflyway display database migration path from the ConfigurableEnvironment from Spring If any, Yeshealth displays the health information of the application (a simple "status" when accessed using an unauthenticated connection, and full information details when using authenticated connection access) Yesinfo displays any application information Yesliquibase shows any Liquibase database migration path Yesmetrics displays metrics information for the current application Yesmappings displays a collection list of all @ RequestMapping paths Yesscheduledtasks displays scheduled tasks in the application Yessessions allows you to retrieve and delete (retrieval and deletion) user sessions from the session store supported by Spring sessions. Not available when using Spring Session support for reactive Web applications. Yesshutdown allows applications to gracefully turn off (not enabled by default) Nothreaddump to execute a thread dumpYes if you use a web application (Spring MVC, Spring WebFlux, or Jersey), you can also use the following endpoints: ID description enables heapdum by default to return a GZip compressed hprof heap dump file Yesjolokia exposes JMX beans through HTTP (when Jolokia is on the classpath WebFlux is not available) Yeslogfile returns the contents of the log file (if the logging.file or logging.path attribute is set), which supports the use of HTTP Range headers to receive partial information about the contents of the log file Yesprometheus displays the metrics information in a format that can be grabbed by the Prometheus server which endpoints Yes wants to change and expose Use the following technology-specific include and exclude properties: PropertyDefaultmanagement.endpoints.jmx.exposure.exclude*management.endpoints.jmx.exposure.include*management.endpoints.web.exposure.exclude*management.endpoints.web.exposure.includeinfo, health

The include property lists the ID of the exposed endpoint

The exclude property lists the ID of endpoints that should not be exposed

The exclude attribute takes precedence over the include attribute. Both the include and exclude attributes can be configured using the endpoint ID list.

* can be used to select all endpoints.

For example, to expose everything except env and beans endpoints through HTTP, use the following properties:

Management.endpoints.web.exposure.include=*management.endpoints.web.exposure.exclude=env,beans III. Turn off authentication

Generally, we use actuator in conjunction with spring security authentication components to prevent these function ports from being called casually. Since this is a functional demonstration, let go of the permission authentication of actuator-related ports first.

In addition, if a Spring Security exists, you need to add a custom security configuration to allow unauthenticated access to endpoints, as shown in the following example: let go of all Endpoint endpoints to match

@ Configurationpublic class ActuatorSecurity extends WebSecurityConfigurerAdapter {@ Overrideprotected void configure (HttpSecurity http) throws Exception {http.requestMatcher (EndpointRequest.toAnyEndpoint ()) .authorizeRequests () .anyRequest () .permitAll ()} 4. View the log level through the / loggers port

Request link: http://localhost:8090/message/loggers

Note that as I said above, I adjusted the management.endpoints.web.base-path=/message. If this parameter is not set, the default / actuator is used to access it.

Initiate a http request to modify the log level

Here is a demonstration of changing the log level of the directory com.wxswj.provider.message.controller to debug

The request type is POST and the parameter format is JSON

Curl-H "Content-Type: application/json"-X POST-- data "{" configuredLevel ":" DEBUG "}" http://localhost:8090/message/loggers/com.wxswj.provider.message.controller

You can initiate a http request on the server through curl, or you can initiate a request through Postman.

Curl-H "Content-Type: application/json"-X POST-- data "{" configuredLevel ":" DEBUG "}" http://localhost:8090/loggers/com.wxswj.provider.message.controller VI. Query log level modification result

Http://localhost:8090/message/loggers/com.wxswj.provider.message.controller

{"configuredLevel": "DEBUG", "effectiveLevel": "DEBUG"}

Indicates that our request to modify the log level takes effect.

This is the end of the article on "what is the operation of SpringBoot dynamic modification log level?" Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "what is the operation of SpringBoot dynamic modification log level". If you want to learn more knowledge, you are welcome to follow the industry information channel.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report