Log Java NullPointerException stack trace

At Spring Boot Whenever a bad Nullpo (the one who made the bug is really bad) comes out, I'm in trouble because I can't keep up with it, so I decided to attach a handler that issues a stack trace to the bottom of the Controller. Surprisingly useful for debugging.

@ExceptionHandler({NullPointerException.class})
public String handleRunTimeException(NullPointerException ex) throws IOException {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    ex.printStackTrace(pw);
    String stackTraceString = sw.toString();
    log.error(stackTraceString);
    sw.close();
    pw.close();
    return "redirect:error/500";
}

Recommended Posts

Log Java NullPointerException stack trace
Get caller information from stack trace (java)
About Java log output
[Java] Filter stack traces
HttpUrlConnection mysterious stack trace
[Java] Stack area and static area
Java standard log output sample
[Java] Collection-List / Set / Map / Stack / Queue
Java reference mechanism (stack and heap)
Log output to file in Java