Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell whether my Spring boot application is in debug mode?

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?

like image 909
Adam Arold Avatar asked Jun 19 '17 11:06

Adam Arold


People also ask

How do I check spring boot application status?

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 .

How do I show debug logs in spring boot?

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.


1 Answers

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"));
}
like image 164
Andy Wilkinson Avatar answered Oct 15 '22 18:10

Andy Wilkinson