Set Logging Level in Spring Boot via Environment Variable

后端 未结 10 1015
刺人心
刺人心 2020-12-23 08:57

Should it be possible to set logging levels through environment variables alone in a Spring Boot application?

I don\'t want to use application.properties

相关标签:
10条回答
  • 2020-12-23 09:56

    Folks can anyone explain why this is not working?

    $ export LOGGING_LEVEL_COM_ACME=ERROR

    For all other configuration using environment variables as an override seems to work with no issues, for example:

    $ export EUREKA_CLIENT_ENABLED=false

    Thanks.

    0 讨论(0)
  • 2020-12-23 09:57

    I also tried to set logging level via environment variable but as already mentioned it is not possible by using environment variable with upper case name, eg. LOGGING_LEVEL_ORG_SPRINGFRAMEWORK=DEBUG. I also didn't want to do it via application.properties or _JAVA_OPTIONS.

    After digging into class org.springframework.boot.logging.LoggingApplicationListener I've checked that spring boot tries to set logging level DEBUG to ORG_SPRINGFRAMEWORK package which is not real package name. So conclusion is that you can use environment variable to set logging level but it needs to be in the form: LOGGING_LEVEL_org.springframework=DEBUG or logging.level.org.springframework=DEBUG

    Tested on spring boot 1.5.3

    0 讨论(0)
  • 2020-12-23 10:01

    I would anyway suggest you to use Spring profiles:

    1. Create 2 properties files:

      application-local.properties and application-remote.properties

      (profile names can be different obviously)

    2. Set the logging level in each file accordingly (logging.level.org.springframework)

    3. Run your application with -Dspring.profiles.active=local locally and -Dspring.profiles.active=remote for CF.

    0 讨论(0)
  • 2020-12-23 10:03

    Also using Spring Boot (v1.2.3) in Cloud Foundry, I've found that it is possible to adjust the root logging level using an environment variable as follows:

    $ cf set-env <app name> LOGGING_LEVEL_ROOT DEBUG
    

    Unfortunately, it does not appear to be possible to dial-down the logging level for specific packages (at least with the version of Java Buildpack and Spring Boot I am using). For example adding the following in addition to the above does not reduce the log level for Spring framework:

    $ cf set-env <app name> LOGGING_LEVEL_ORG_SPRINGFRAMEWORK INFO
    

    If you are using something like Splunk to gather your logs, you may be able to filter out the noise, however.

    Another alternative which looks promising could be based on customisation of the build pack's arguments option (see here):

    $ cf set-env <app name> '{arguments: "-logging.level.root=DEBUG -logging.level.org.springframework=INFO"}'
    

    Sadly, I couldn't get this to actually work. I certainly agree that being able to reconfigure logging levels at package level without changing the application code would be handy to get working.

    0 讨论(0)
提交回复
热议问题