springboot-Change log level to DEBUG at runtime by springboot actuator
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:
You can see that, this command is just printing logs with DEBUG and INFO level.
And we have this settings in application.properties:
So , we only get this logs over and over:
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:
Then add this property to your application.properties:
Finally, you can access the actuator endpoints to change the logging level like this:
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 :
It works!
4. For SpringBoot 2.x
For SpringBoot 2+ users, first, you should add this dependency:
Then add this property to your application.properties:
Finally, you can access the actuator endpoints to change the logging level like this:
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 :
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.