Determining if the Spring Boot Application is Running in Debug or Trace Mode

Featured image for sharing metadata for article

As with any software development process, we can't always build things right the first time, and sometimes we need to up the logging levels, or debug our framework a little more in depth.

When running Spring Boot applications, we may want to set the debug/trace modes to get a bit more information from Spring Boot, and the various libraries bundled with it.

Debug mode

Setting debug mode

You can do this by running in debug mode, which is most commonly done by setting the debug command-line flag:

java -jar /path/to/boot.jar --debug

Or by specifying in our properties file that we're using debug mode:

debug=true

Determining debug mode

In the case that we want to work out whether we're in debug mode, for instance to increase logging configuration by adding new beans to the classpath, or i.e. add a wiretap to our WebClient.

To do so, we can use the following SPring Expression Language (SPEL) to

import org.springframework.core.env.Environment;
import org.springframework.beans.factory.annotation.Value;

public WebClient webClient(
      @Value("#{environment.getProperty('debug') != null && environment.getProperty('debug') != 'false'}")
        boolean isDebug
      ) {
  if (isDebug) {
    // ...
  }

  // return
}

Alternatively, you may want to define a bean for it, so there's only a single means of performing this lookup:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;

@Bean
public boolean isDebug(
    @Value("#{environment.getProperty('debug') != null && environment.getProperty('debug') != 'false'}")
        boolean isDebug) {
  return isDebug;
}

@Bean
public WebClient webClient(boolean isDebug) {
  if (isDebug) {
    // ...
  }

  // return
}

This could also be improved to allow you to enforce that debug mode cannot be turned on production environments.

If you'd like to avoid SPEL, you can also inject the Environment itself, and query it manually:

@Bean
public boolean isDebug(Environment environment) {
  String debug = environment.getProperty("debug");
  return null != debug && !debug.equals("false");
}

Trace Mode

You can do this by running in trace mode, which is most commonly done by setting the trace command-line flag:

java -jar /path/to/boot.jar --trace

Or by specifying in our properties file that we're using trace mode:

trace=true

Determining trace mode

In the case that we want to work out whether we're in trace mode, for instance to increase logging configuration by adding new beans to the classpath, or i.e. add a wiretap to our WebClient.

To do so, we can use the following SPring Expression Language (SPEL) to

import org.springframework.core.env.Environment;
import org.springframework.beans.factory.annotation.Value;

public WebClient webClient(
      @Value("#{environment.getProperty('trace') != null && environment.getProperty('trace') != 'false'}")
        boolean isTrace
      ) {
  if (isTrace) {
    // ...
  }

  // return
}

Alternatively, you may want to define a bean for it, so there's only a single means of performing this lookup:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;

@Bean
public boolean isTrace(
    @Value("#{environment.getProperty('trace') != null && environment.getProperty('trace') != 'false'}")
        boolean isTrace) {
  return isTrace;
}

@Bean
public WebClient webClient(boolean isTrace) {
  if (isTrace) {
    // ...
  }

  // return
}

This could also be improved to allow you to enforce that trace mode cannot be turned on production environments.

If you'd like to avoid SPEL, you can also inject the Environment itself, and query it manually:

@Bean
public boolean isTrace(Environment environment) {
  String trace = environment.getProperty("trace");
  return null != trace && !trace.equals("false");
}

Notes

Note that the debug check above only returns true if we're in debug mode, not in trace mode. You may want to adapt the isDebug definition to cater for both, unless there is different processing you wish to do between debug and trace.

Written by Jamie Tanna's profile image Jamie Tanna on , and last updated on .

Content for this article is shared under the terms of the Creative Commons Attribution Non Commercial Share Alike 4.0 International, and code is shared under the Apache License 2.0.

#blogumentation #java #spring-boot.

Also on:

This post was filed under articles.

Interactions with this post

Interactions with this post

Below you can find the interactions that this page has had using WebMention.

Have you written a response to this post? Let me know the URL:

Do you not have a website set up with WebMention capabilities? You can use Comment Parade.