Java Throwable to String formatException(Throwable e)

Here you can find the source of formatException(Throwable e)

Description

Formats an exception as a string for logging purposes

Necessary because slf4j does not by default have a method for logging exceptions

License

Open Source License

Parameter

Parameter Description
e Exception

Return

String for logging purposes

Declaration

public static String formatException(Throwable e) 

Method Source Code


//package com.java2s;
import java.io.PrintWriter;
import java.io.StringWriter;

public class Main {
    /**// w w  w .ja v  a 2 s . co  m
     * Formats an exception as a string for logging purposes
     * <p>
     * Necessary because slf4j does not by default have a method for logging
     * exceptions
     * </p>
     * 
     * @param e
     *            Exception
     * @return String for logging purposes
     */
    public static String formatException(Throwable e) {
        StringWriter writer = new StringWriter();
        PrintWriter pwriter = new PrintWriter(writer);
        writer.append(e.getClass().getCanonicalName());
        if (e.getMessage() != null) {
            writer.append(": ");
            writer.append(e.getMessage());
        }
        writer.append('\n');
        e.printStackTrace(pwriter);
        pwriter.flush();
        return writer.toString();
    }
}

Related

  1. formatException(Throwable t)
  2. formatException(Throwable t)
  3. formatExceptionForDisplay(Exception e)
  4. formatThrowableForHtml(Throwable t)