1. Introduction

This article would demo how to change logger level to DEBUG at runtime by springboot acutator http endpoints.

2. Environments

  • SpringBoot 1.5.1+ or SpringBoot 2.x

We have a class that print logs to change at runtime, the code is:

package com.bswen.sb1jt.commands;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class MyLoggerCommand implements CommandLineRunner {
    private static Logger logger = LoggerFactory.getLogger(MyLoggerCommand.class);


    @Override
    public void run(String... strings) throws Exception {
        while(true) {
            Thread.sleep(1000);

            logger.debug("debug level log");
            logger.info("info level log");

        }
    }
}

You can see that, this command is just printing logs with DEBUG and INFO level.

And we have this settings in application.properties:

logging.level.=ERROR
logging.level.com.bswen.sb1jt=INFO

So , we only get this logs over and over:

2019-06-02 21:17:30,928 INFO  [main] com.bswen.sb1jt.commands.MyLoggerCommand: info level log
2019-06-02 21:17:31,930 INFO  [main] com.bswen.sb1jt.commands.MyLoggerCommand: info level log
2019-06-02 21:17:32,931 INFO  [main] com.bswen.sb1jt.commands.MyLoggerCommand: info level log
2019-06-02 21:17:33,937 INFO  [main] com.bswen.sb1jt.commands.MyLoggerCommand: info level log
2019-06-02 21:17:34,940 INFO  [main] com.bswen.sb1jt.commands.MyLoggerCommand: info level log
2019-06-02 21:17:35,943 INFO  [main] com.bswen.sb1jt.commands.MyLoggerCommand: info level log
2019-06-02 21:17:36,949 INFO  [main] com.bswen.sb1jt.commands.MyLoggerCommand: info level log
2019-06-02 21:17:37,950 INFO  [main] com.bswen.sb1jt.commands.MyLoggerCommand: info level log

Now we want to show the DEBUG level logs, how to do it without restarting the app?

3. For springboot 1.5.1+

For SpringBoot 1.5.1+ users, first, you should add this dependency:

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

Then add this property to your application.properties:

management.security.enabled=false 

Finally, you can access the actuator endpoints to change the logging level like this:

curl -X "POST" "http://localhost:8080/loggers/com.bswen.sb1jt" -H "Content-Type: application/json; charset=utf-8" -d $'{"configuredLevel": "DEBUG"}'
  • By default 8080 port:

    In a standalone application, the Actuator HTTP port defaults to the same as the main HTTP port. To make the application listen on a different port, set the external property: management.server.port. To listen on a completely different network address (such as when you have an internal network for management and an external one for user applications), you can also set management.server.address to a valid IP address to which the server is able to bind.

  • the url loggers/com.bswen.sb1jt is the logger you want to change, com.bswen.sb1jt is your package name.

Then you can see the logging outputs, it changes to :

2019-06-02 21:17:36,949 INFO  [main] com.bswen.sb1jt.commands.MyLoggerCommand: info level log
2019-06-02 21:17:37,950 INFO  [main] com.bswen.sb1jt.commands.MyLoggerCommand: info level log
2019-06-02 21:17:38,955 DEBUG [main] com.bswen.sb1jt.commands.MyLoggerCommand: debug level log
2019-06-02 21:17:38,956 INFO  [main] com.bswen.sb1jt.commands.MyLoggerCommand: info level log
2019-06-02 21:17:39,957 DEBUG [main] com.bswen.sb1jt.commands.MyLoggerCommand: debug level log
2019-06-02 21:17:39,957 INFO  [main] com.bswen.sb1jt.commands.MyLoggerCommand: info level log
2019-06-02 21:17:40,958 DEBUG [main] com.bswen.sb1jt.commands.MyLoggerCommand: debug level log

It works!

4. For SpringBoot 2.x

For SpringBoot 2+ users, first, you should add this dependency:

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

Then add this property to your application.properties:

management.endpoints.web.exposure.include=*

Finally, you can access the actuator endpoints to change the logging level like this:

curl -X "POST" "http://localhost:8080/actuator/loggers/com.bswen.sb1jt" -H "Content-Type: application/json; charset=utf-8" -d $'{"configuredLevel": "DEBUG"}'
  • By default 8080 port:

    In a standalone application, the Actuator HTTP port defaults to the same as the main HTTP port. To make the application listen on a different port, set the external property: management.server.port. To listen on a completely different network address (such as when you have an internal network for management and an external one for user applications), you can also set management.server.address to a valid IP address to which the server is able to bind.

  • the url /actuator/loggers/com.bswen.sb1jt is the logger you want to change, com.bswen.sb1jt is your package name. And notice the prefix /actuator, it’s different from the SpringBoot 1.5.x

Then you can see the logging outputs, it changes to :

2019-06-02 21:17:36,949 INFO  [main] com.bswen.sb1jt.commands.MyLoggerCommand: info level log
2019-06-02 21:17:37,950 INFO  [main] com.bswen.sb1jt.commands.MyLoggerCommand: info level log
2019-06-02 21:17:38,955 DEBUG [main] com.bswen.sb1jt.commands.MyLoggerCommand: debug level log
2019-06-02 21:17:38,956 INFO  [main] com.bswen.sb1jt.commands.MyLoggerCommand: info level log
2019-06-02 21:17:39,957 DEBUG [main] com.bswen.sb1jt.commands.MyLoggerCommand: debug level log
2019-06-02 21:17:39,957 INFO  [main] com.bswen.sb1jt.commands.MyLoggerCommand: info level log
2019-06-02 21:17:40,958 DEBUG [main] com.bswen.sb1jt.commands.MyLoggerCommand: debug level log

It works!

5. Summary

It’s easy to change the logging level at runtime by using SpringBoot actuator, but you should notice the security issues of the actuator endpoints. I would introduce more security features of SpringBoot actuators in the future.