Set Logging Level in Spring Boot via Environment Variable
This is just an idea, but did you try setting
_JAVA_OPTIONS=-Dlogging.level.org.springframework=TRACE
?
Theoretically, this way -Dlogging.level.org.springframework=TRACE
will be passed as default JVM argument and should affect every JVM instance in your environment.
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 visible for the LoggingApplicationListener: The environment variable for MyClass
has to be defined as MYCLASS
and will be available as myclass
in Spring's Environment (this example ignores the fully-qualified name of the class).
Once the camel case notation of the class is lost, during runtime there's no chance to recover the original class name. Thus log definitions in environment variables don't work for classes but only for packages.
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