Prevent stack trace logging for custom exception in Spring Boot application Prevent stack trace logging for custom exception in Spring Boot application spring spring

Prevent stack trace logging for custom exception in Spring Boot application


Be wary of Spring Boot DevTools.

Despite NEVER being the default for server.error.include-stacktrace, if you include Spring Boot DevTools it overides to ALWAYS.

If you're interested in more detail, see this commit, which became part of Spring Boot 2.1.0+enter image description here


If you don't need the stack trace you can suppress the stack trace by overriding fillInStackTrace in your exception class.

public class DuplicateFoundException extends RuntimeException {    @Override    public synchronized Throwable fillInStackTrace() {        return this;    }}

When you invoke e.printStackTrace() then no stack trace will be printed.

See also this blog post.