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

后端 未结 10 1000
北荒
北荒 2020-12-23 19:55

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

10条回答
  •  天命终不由人
    2020-12-23 20:20

    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:

    
      
          
    
    

    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.

提交回复
热议问题