Disable stack trace logging for custom exception in Spring Appload

Is there a way in Spring Boot (mvc) to register a custom exception and throw it if its stack trace is not visible in the log file ? But for any other exception, they still see the stack trace.

Long explanation:

I use Spring Boot to create a simple rest service. I like that for custom exceptions there is no stack trace in the logs by default , and a json response is created with basic information about the exception (status, error, message).

The problem is that it does not create a log entry at all , so I would have to do it manually:

Custom exception

@ResponseStatus(value = HttpStatus.CONFLICT) public class DuplicateFoundException extends RuntimeException { public DuplicateFoundException(String message) { super(message); } } 

Throw exception in service method (in @RestController)

 if (!voteDao.findByItemAndUser(item, voteDto.getUserId()).isEmpty()) { log.warn("... already voted ..."); //TODO: don't do this for every throw throw new DuplicateFoundException("... already voted ..."); } 

Having more exceptions causes the log statement to be placed before each throw, which I consider to be bad. I tried removing all the log statements from the service method and created @ControlledAdvice where I would log all user exceptions and just throw them to still get good json:

 @ControllerAdvice public class RestExceptionHandler { private static final Logger log = Logger.getLogger(RestExceptionHandler.class); @ExceptionHandler public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception { if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) != null) { log.warn(e.getMessage()); } else { log.error("..."); } throw e; } } 

Now the problem is that I see not only the log entry, but also the stack trace for user exceptions and cannot find a way to prevent this. I think the problem is because she throws it again. A possible solution would be to create a custom class for the exception, which I will return instead, but I don't like the idea, since the marshalling exception seems to work fine.

Any clues? Thanks.

+15
java spring spring-boot logging exception-handling
source share
4 answers

The solution was to leave exception handling in spring boot , so the custom exception is not logged and any other is logged by default. I removed @ControllerAdvice, as well as the log statements from the rest of the controller and the added log statement for the custom exception constructor .

 public DuplicateFoundException(String message) { super(message); LOGGER.warn(message); } 

I'm not sure if this is the best approach, but now I have a custom exception logging in only one place and I don’t need to repeat the log statement for each exception or see its stack trace or any other error message in the logs.

+1
source share

I am using Spring Boot 2+ just add this line to your application.properties:

server.error.include-StackTrace = never

https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/autoconfigure/web/ErrorProperties.IncludeStacktrace.html

+15
source share

If you don't need a 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 call e.printStackTrace() , the stack trace will not be printed.

See also this blog post .

+10
source share

Beware of Spring Boot DevTools.

Despite the fact that NEVER used by default for server.error.include-stacktrace , when you enable Spring Boot DevTools, it goes into ALWAYS .

See this commit, which became part of 2.1. 0+

https://github.com/spring-projects/spring-boot/commit/cb621024e480fb08a79277273f81aa169aed14df

0
source share

All Articles