Set Logging Level in Spring Boot via Environment Variable

后端 未结 10 1020
刺人心
刺人心 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:54

    Setting log levels via environment variables can only be done for Packages but not for Classes

    I had the same problem as the OP. And I was wondering why some of the users here reported that the proposed solutions worked well while others responded they didn't.
    I'm on Spring Boot 2.1 and the problem obviously changed a bit over the last years, but the current situation is as follows:

    TL;DR

    Setting the log level for a package works:

    LOGGING_LEVEL_COM_ACME_PACKAGE=DEBUG
    

    While setting the log level for a specific class has no effect:

    LOGGING_LEVEL_COM_ACME_PACKAGE_CLASS=DEBUG
    

    How can that be?

    Have a look at Spring Boot's LoggingApplicationListener.
    If you'd debug it and set a breakpoint in the highlighted code block, you'd see that the log level definition for a class com.acme.mypackage.MyClass becomes com.acme.mypackage.myclass.
    So a log level definition for a class looks exactly like a log level definition for a package.

    This is related to Spring's Relaxed Binding, which proposes an upper case notation for environment variables. Thus the typical camel case notation of a class is not available for the LoggingApplicationListener. On the other hand, I think one can't check whether the given fully-qualified path matches a class. The classloader won't find anything as long as the notation doesn't match exactly.

    Thus log definitions in environment variables don't work for classes but only for packages.

提交回复
热议问题