2 Star 1 Fork 0

基础平台组件 / frame

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
ThrowableInfoHelper.java 2.97 KB
一键复制 编辑 原始数据 按行查看 历史
fuc 提交于 2017-04-13 10:20 . 框架支持包更新
package cn.platform.agent.frame.core.helper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.*;
import java.util.ArrayList;
/**
* <p>标题: </p>
* <p>功能描述: </p>
* <p>创建时间:17/4/12 </p>
* <p>作者:SIVEN</p>
* <p>修改历史记录:</p>
* ============================================================<br>
*/
public class ThrowableInfoHelper {
protected final Log logger = LogFactory.getLog(getClass());
/**
* =======================================================================
* 获得操作系统中,换行字符串。如Micorsoft Windows XP专业版中换行符"\r\n"
* =======================================================================
*/
// Note that the line.separator property can be looked up even by applets.
// 参考org.apache.log4j.Layout
public final static String LINE_SEP = System.getProperty("line.separator");
// 参考java.io.BufferedWriter
public final static String LINE_SEP2 = (String) java.security.AccessController
.doPrivileged(new sun.security.action.GetPropertyAction(
"line.separator"));
/**
* @param throwable 异常对象
* @return
* @Description:将Throwable对象的错误堆栈内容形成字符串<br> 参考
* {@link org.apache.log4j.spi.ThrowableInformation}
* 代码
* @author mahh
* @since:2014-9-30 下午02:32:51
*/
public static String[] getThrowableStrRep(Throwable throwable) {
if (throwable == null) {
return new String[0];
}
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
throwable.printStackTrace(pw);
pw.flush();
LineNumberReader reader = new LineNumberReader(new StringReader(
sw.toString()));
ArrayList lines = new ArrayList();
try {
String line = reader.readLine();
while (line != null) {
lines.add(line);
line = reader.readLine();
}
} catch (IOException ex) {
lines.add(ex.toString());
}
String[] rep = new String[lines.size()];
lines.toArray(rep);
return rep;
}
public static String getThrowableStr(Throwable throwable) {
StringBuffer throwableStr = new StringBuffer();
String[] errorStrArray = getThrowableStrRep(throwable);
for (int i = 0; i < errorStrArray.length; i++) {
throwableStr.append(errorStrArray[i]).append(LINE_SEP2);
}
return throwableStr.toString();
}
// 测试代码
public static void main(String[] args) {
RuntimeException e1 = new RuntimeException("aaa");
RuntimeException e2 = new RuntimeException("e2", e1);
RuntimeException e3 = new RuntimeException(e2);
System.out.println("=====");
String throwableStr = getThrowableStr(e3);
System.out.println(throwableStr);
}
}
Java
1
https://gitee.com/platform-agent/frame.git
git@gitee.com:platform-agent/frame.git
platform-agent
frame
frame
829be16758b0d5813ee2a75a751149b06010cc6c

搜索帮助