Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I change log level in runtime without restarting spring boot application

I have deployed springboot application in PCF . I want to log the message based on the environment variable .What should I do so that the run time log level change will work without restarting the application?

like image 970
Samir Padhy Avatar asked Nov 21 '15 14:11

Samir Padhy


People also ask

Can we change log level at runtime?

In terms of scaling, the challenge is to change log levels in each instance. So to avoid all pitfalls following is needed: Dynamically change the log level at runtime without application restart. Propagation of log level changes across the application instances.

How do I change the logger level in spring boot?

In Settings -> Config Vars set logging. level.com. yourpackage to the desired level (INFO, ERROR, DEBUG).

How do you change the logging level?

To change log levels as a root user, perform the following: To enable debug logging, run the following command: /subsystem=logging/root-logger=ROOT:change-root-log-level(level=DEBUG) To disable debug logging, run the following command: /subsystem=logging/root-logger=ROOT:change-root-log-level(level=INFO)


2 Answers

Changing the log level in Spring Boot 1.5+ can be done with a http-endpoint

Add

<dependency>     <groupId>org.springframework.boot</groupId>     <artifactId>spring-boot-starter-actuator</artifactId> </dependency> 

and than you can use

curl -X "POST" "http://localhost:8080/loggers/de.springbootbuch" \      -H "Content-Type: application/json; charset=utf-8" \      -d $'{   "configuredLevel": "WARN" }'   

Where everything beyond /loggers/ is the name of the logger.

If you running this in PCF it get's even better: This is directly supported from their backend.

like image 135
Michael Simons Avatar answered Oct 05 '22 17:10

Michael Simons


For Spring Boot 2.1.5+:

First, you need the actuator Plugin:

<dependency>     <groupId>org.springframework.boot</groupId>     <artifactId>spring-boot-starter-actuator</artifactId> </dependency> 

Second, you need to expose the endpoint like Dennis said in his comment (loggers is disabled by default):

management.endpoints.web.exposure.include=health,info,loggers 

Finally, you can use the Rest Endpoints to get Information about the loggers and set the logging levels.

curl -X "GET" "http://localhost:8080/actuator/loggers" 

To set the Root logging Level you can use

curl -X "POST" "http://localhost:8080/actuator/loggers/ROOT" -H "Content-Type: application/json; charset=utf-8"   -d $'{ "configuredLevel": "INFO" }' 
like image 24
Alexander Weiß Avatar answered Oct 05 '22 18:10

Alexander Weiß