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?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
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.
First you need to include some dependencies
Enable your app to be a Spring Boot Admin Server using the annotation
@EnableAdminServer
.In your
application.properties
add the following:Register your app to the Spring Boot Admin Server which is still your app
Instruct Spring Boot Admin Server where to find the client
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
Since Spring Boot 1.5.x, you can use logger endpoint to POST desired logging level.
Changing the log level in Spring Boot 1.5+ can be done with a http-endpoint
Add
and than you can use
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.
If you are using
logback
api to configure logging in the project then you can use the AutoScan feature oflogback
api. As per documentationPlease go through
logback
API documentation for more details.If you use Log4j 2 for logging you can easily configuration it to set the log level to use based on an environment variable or system property. If you do it this way you won't need to modify the file just because the environment changed.
You can also add a settings page in the web service to update the log level. This can then be done using ajax. The following example includes login and csrf token:
First, add some form to specify the new log level. Can be improved for example by using a
select
element.Then, the request is sent:
The following function injects the csrf token to the POST request: