Prevent stack trace logging for custom exception in Spring Boot application

前端 未结 4 1074
走了就别回头了
走了就别回头了 2021-02-01 17:06

Is there a way in Spring Boot (mvc) to log a custom exception and throw it without its stack trace being visible in the log file? But for any other exception st

4条回答
  •  不思量自难忘°
    2021-02-01 17:41

    Solution was to leave the exception handling to spring boot, so that custom exception is not logged and any other is logged by default. I removed the @ControllerAdvice and also the logging statements from the rest controller and added logging statement to custom exception constructor.

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

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

提交回复
热议问题