Prevent stack trace logging for custom exception in Spring Boot application

前端 未结 4 1072
走了就别回头了
走了就别回头了 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:17

    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.

提交回复
热议问题