En esta ocasión les traigo una clase utilitaria que me sirvio para convertir el StackTrace de una exception a una cadena de String, esto surgio por la necesidad de enviar por correo electronico el StackTrace del errror generado a mi cuenta en gmail.
public final class StackTraceUtil {
public static String getStackTrace(Throwable aThrowable) {
final Writer result = new StringWriter();
final PrintWriter printWriter = new PrintWriter(result);
aThrowable.printStackTrace(printWriter);
return result.toString();
}
public static String getCustomStackTrace(Throwable aThrowable) {
final StringBuilder result = new StringBuilder("ERROR: ");
result.append(aThrowable.toString());
final String NEW_LINE = System.getProperty("line.separator");
result.append(NEW_LINE);
for (StackTraceElement element : aThrowable.getStackTrace()) {
result.append(element);
result.append(NEW_LINE);
}
return result.toString();
}
}
Referencias: javapractices
public static String getStackTrace(Throwable aThrowable) {
final Writer result = new StringWriter();
final PrintWriter printWriter = new PrintWriter(result);
aThrowable.printStackTrace(printWriter);
return result.toString();
}
public static String getCustomStackTrace(Throwable aThrowable) {
final StringBuilder result = new StringBuilder("ERROR: ");
result.append(aThrowable.toString());
final String NEW_LINE = System.getProperty("line.separator");
result.append(NEW_LINE);
for (StackTraceElement element : aThrowable.getStackTrace()) {
result.append(element);
result.append(NEW_LINE);
}
return result.toString();
}
}
Referencias: javapractices
No hay comentarios:
Publicar un comentario