Search code examples
javaexceptionstacktrace

Minimize content of the Java stack trace


I'm looking for an approach that would give me the flexibility to specify what should be printed. For example, I want my stack trace to only have the filename:line_number printed. I don't want the package name and function name.


Solution

  • You can write an own utility method for that:

    Given an Throwable e get the list of StackTraceElement

    StackTraceElement[] elems = e.getStackTrace();
    

    then loop over the elements and print the information you want, using StackTraceElement.getFileName() and getLineNumber()