All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.zxyinfo.logger.logback.util.ThrowableUtils Maven / Gradle / Ivy

There is a newer version: 1.0.2
Show newest version
package com.zxyinfo.logger.logback.util;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * @author joewee
 * @version 1.0.0
 * @date 2021/10/29 10:32
 */
public class ThrowableUtils {
  private ThrowableUtils() {
  }

  public static Throwable findRootCause(Throwable cause) {
    Throwable rootCause;
    for(rootCause = cause; rootCause != null && rootCause.getCause() != null; rootCause = rootCause.getCause()) {
    }

    return rootCause;
  }

  public static String printStackTrace(Throwable cause, boolean rootCauseOnly) {
    if (rootCauseOnly) {
      cause = findRootCause(cause);
    }

    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    if (cause != null) {
      cause.printStackTrace(pw);
    }

    return sw.toString();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy