How to get Environment properties from application.properties into logback.groovy in Spring Boot project?

前端 未结 2 552
悲&欢浪女
悲&欢浪女 2021-02-14 23:48

Trying to inject properties defined in application.properties/application.yml into logback.groovy script in Spring Boot proje

2条回答
  •  死守一世寂寞
    2021-02-15 00:32

    logback.groovy needs to be evaluated very early because otherwise the code for loading the spring configuration, instantiating beans, etc. could not log anything. That's why @Inject can't work.

    I see 2 options:

    1. You could easily load application.properties in logback.groovy, but it's far from trivial to take all the configuration override mechanisms that spring provides into account
    2. Create a spring bean that changes the logback configuration programmaticaly when initialized

    A different approach is to externalize the logback configuration on production with -Dlogback.configurationFile=/path/to/config.groovy. Putting the config file in a well known location (like /etc/my-app) makes it easy to change log levels in production without re-deploying (or even re-starting).

提交回复
热议问题