I'd like to modify how my application works depending on whether the --debug
switch is present or not. I tried this in my @Configuration
file:
@Value("\${debug}")
lateinit var debug: String
but Spring says
Could not resolve placeholder 'debug' in value "${debug}"
How can I query the state of the --debug
option?
On system level - you can run your project as a service, which is documented in the Official documentation - Deployments. Then you can query the application status service myapp status .
You can enable debug logging by specifying --debug when starting the application from the command-line. Spring Boot provides also a nice starting point for logback to configure some defaults, coloring etc. the base. xml file which you can simply include in your logback.
The most robust way to check for debug mode is to query the Environment
. This will allow you to detect that the mode has been enabled whether that's been done via a command line argument (--debug
), system property (-Ddebug
), environment variable (DEBUG=true
), etc.
You can inject an instance of the Environment
as you would any other dependency or you can implement EnvironmentAware
. The getProperty(String)
method can then be used to retrieve the value of the debug
property. Spring Boot treats debug
as being enabled if the debug
property has a non-null value other than false
:
private boolean isSet(ConfigurableEnvironment environment, String property) {
String value = environment.getProperty(property);
return (value != null && !value.equals("false"));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With