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

66,129

Solution 1

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.

Solution 2

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" }'

Solution 3

This is an extension of @Michael Simons answer. With this method you will have a UI for doing that:

This method is a bit longer but it solves much much more. We are going to use a tool called Spring Boot Admin Server.

  1. First you need to include some dependencies

    <!--Dependency for registering your app as a Spring Boot Admin Server-->
    <dependency>
        <groupId>de.codecentric</groupId>
        <artifactId>spring-boot-admin-server</artifactId>
        <version>1.3.3</version>
    </dependency>
    
    <!--Provide a nice looking ui-->
    <dependency>
        <groupId>de.codecentric</groupId>
        <artifactId>spring-boot-admin-server-ui</artifactId>
        <version>1.3.3</version>
    </dependency>
    
    <!--Dependency for registering your app as a Spring Boot Admin Client-->
    <dependency>
        <groupId>de.codecentric</groupId>
        <artifactId>spring-boot-admin-starter-client</artifactId>
        <version>1.3.0</version>
    </dependency>
    <dependency>
            <groupId>org.jolokia</groupId>
            <artifactId>jolokia-core</artifactId>
    </dependency>
    
  2. Enable your app to be a Spring Boot Admin Server using the annotation @EnableAdminServer.

    @SpringBootApplication
    @EnableAdminServer
    public class Application {
       public static void main(String[] args) {
          // ... your code as before ...
       }
    }
    
  3. In your application.properties add the following:

    Register your app to the Spring Boot Admin Server which is still your app

    spring.boot.admin.url=http://localhost:8031
    

    Instruct Spring Boot Admin Server where to find the client

    // For versions 2.*.*
    spring.boot.admin.client.url=http://localhost:8031
    // For versions 1.*.*
    spring.boot.admin.client.service-url=http://localhost:8031
    spring.boot.admin.client.management-url=http://localhost:8031
    spring.boot.admin.client.health-url=http://localhost:8031/health
    
  4. In your logback.xml just add the following line <jmxConfigurator/>. This allows configuration of logback via JMX. More info here

... and voila you are done. Now you can change the debug level for any logger at runtime.

i. Just visit the url for your Spring Boot Admin Server-in our case here (http:/localhost:8031).

ii. A list of applications (clients) registered will be displayed on the home page.

iii. Click Details against the registered clients which will take you to another page.

iv. Click the Logging tab which will list all loggers registered in your application.

v. You can change the log levels it will change your logging level at runtime. Here is a snippet of what you expect

Change logging levels at runtime

Solution 4

If you are using logback api to configure logging in the project then you can use the AutoScan feature of logback api. As per documentation

logback-classic will scan for changes in its configuration file and automatically reconfigure itself when the configuration file changes. In order to instruct logback-classic to scan for changes in its configuration file and to automatically re-configure itself set the scan attribute of the element to true.

<configuration scan="true"> 
  ... 
</configuration> 

Scan frequency: "By default, the configuration file will be scanned for changes once every minute". See the logback API documentation for more details.

Solution 5

Since Spring Boot 1.5.x, you can use logger endpoint to POST desired logging level.

Share:
66,129
Admin
Author by

Admin

Updated on August 26, 2021

Comments

  • Admin
    Admin almost 3 years

    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?

  • Kevin Hooke
    Kevin Hooke over 6 years
    It's worth emphasizing that the Actuator loggers endpoint was added in 1.5.1 and if you have an older Spring Boot app using < 1.5.1 with Actuator, even though there are other Actuator endpoints provided, you won't find the one for loggers. I know because I just spent ages trying to work out why it wouldn't turn on :-)
  • Do Will
    Do Will over 5 years
    Changing system property requires restart.
  • Dennis
    Dennis over 5 years
    For Spring Boot 2.1 you have to make sure that the loggers endpoint is exposed to web by adding loggers value to management.endpoints.web.exposure.include setting. If you don't have such line in your application.properties file, than add: management.endpoints.web.exposure.include=health,info,logger‌​s, since its default is health,info
  • Enrico Giurin
    Enrico Giurin over 5 years
    In this article is described the process: blog.codeleak.pl/2017/03/… . Do not forget to add the attribute scan=true in the logback.xml as written here: stackoverflow.com/a/42575844/379173
  • t0r0X
    t0r0X almost 5 years
    Thanks! The management.endpoints.web.exposure.include=...,loggers property was the missing piece!
  • Peterson V
    Peterson V almost 4 years
    java.lang.ClassCastException: class org.apache.logging.slf4j.Log4jLoggerFactory cannot be cast to class org.apache.logging.log4j.core.LoggerContext