how do I change log level in runtime without resta

2019-02-04 08:23发布

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?

7条回答
兄弟一词,经得起流年.
2楼-- · 2019-02-04 09:14

The default logging provider is logback. To setup your system so that the logging level can be changed at runtime you need to perform the following steps:

Firstly in src/main/resources create a custom logback configuration named logback-spring.xml that includes spring's default configurator and then adds the directive that exposes logback configuration over JMX:

<configuration>
  <include resource="org/springframework/boot/logging/logback/base.xml"/>
  <jmxConfigurator />    
</configuration>

Now add a dependency on the Jolokia JMX-over-HTTP bridge: org.jolokia:jolokia-core.

You should now be able to hit /jolokia endpoints on your spring boot application. The protocol is documented here. It's not pretty. To get you started, here's a few GET examples that you can hit straight from a browser:

Show ROOT logger level:

/jolokia/exec/ch.qos.logback.classic:Name=default,Type=ch.qos.logback.classic.jmx.JMXConfigurator/getLoggerLevel/ROOT

Change ROOT logger level to debug:

/jolokia/exec/ch.qos.logback.classic:Name=default,Type=ch.qos.logback.classic.jmx.JMXConfigurator/setLoggerLevel/ROOT/debug

spring-boot-actuator is aware of the /jolokia endpoint and it is marked sensitive=true so if you have spring-security on the classpath then it will require authentication.

查看更多
登录 后发表回答