Android 怎么把异腟tackTrace信息格式化为String
发布网友
发布时间:2024-10-17 01:54
我来回答
共3个回答
热心网友
时间:2024-11-27 07:50
Throwable ex = new Throwable();
final Writer result = new StringWriter();
final PrintWriter printWriter = new PrintWriter(result);
ex.printStackTrace(printWriter);
String stacktrace = result.toString();
printWriter.close();
热心网友
时间:2024-11-27 07:51
在Android开发中,常常需要把异常信息的StackTrace转化成String写入日志文件中。 最初,我使用getStackTrace().toString() 这样的代码,后来发现这样无法获取全部信息,怎么办呢,经过查阅资料,写出了下面的函数:
public static String FormatStackTrace(Throwable throwable) {
if(throwable==null) return "";
String rtn = throwable.getStackTrace().toString();
try {
Writer writer = new StringWriter();
PrintWriter printWriter = new PrintWriter(writer);
throwable.printStackTrace(printWriter);
printWriter.flush();
writer.flush();
rtn = writer.toString();
printWriter.close();
writer.close();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception ex) {
}
return rtn;
}
由于Exception继承与Throwable,因此,可以直接给该方法传递Exception的对象。
主要适用的类:java.io.Writer 和java.io.StringWriter
java.lang.Exception
热心网友
时间:2024-11-27 07:51
请看以下代码有利于帮助你解决tT信息格式化为String
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN"
"(http://)www(.wapforum).org/DTD/xhtml-(mobile10)(.dtd)" >
<html xmlns="(http:)//(www.)w3.org/1999(/xhtml)">
<head>
<title>应用程序出错</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link rel="stylesheet" href="$link.css("osc-error.css")" type="text/css" media="screen" />
</head>
<body>
#set($__nothing =
$osc_tool.report_error($request))
<div id='msg-box'>
<div class='logo'><a href="$link.root()"><img src="$link.img('logo.gif')" border="0" alt=""></a></div>
<div class='title'>您访问的页面发生错误!</div>
<div class='msg'>我们已经将此错误信息记录下来,并将尽快处理,为此造成您的不便请多见谅</div>
<div class='nav'><a href="$link.root()">> 返回首页 <</a></div>
</div>
</body>
</html>
[代码]web.xml
<error-page>
<error-code>500</error-code>
<location>/500.vm</location>
</error-page>
[代码][Java]代码
/**
* 报告错误信息
* 在vm上的用法
$osc_tool.report_error($request)
* @param t
*/
public static void report_error(HttpServletRequest req){
SmtpHelper.reportError(req, null);
}
SmtpHelper.java
/**
* 报告错误信息
* @param req
* @param excp
*/
public static void reportError(HttpServletRequest req, Throwable
excp){
boolean is_localhost = (req!=null)?"127.0.0.1".equals(RequestUtils.getRemoteAddr(req)):false;
Throwable t = excp;
if(t == null) t = _GetException(req);
if(t == null) return ;
log.error("System Exception", t);
if(!is_localhost)
//发送电子邮件通知
try {
String email = smtp_props.getProperty("administrator");
String title = ResourceUtils.getString("ui", "error_500", t.getClass().getSimpleName());
String content = getErrorHtml(req, t);
//发送邮件到指定邮箱
_SendHtmlMail(Arrays.asList(StringUtils.split(email,",")), title, content);
} catch (Exception e) {
log.error("Failed to send error report.", e);
}
}
/**
* 格式化错误信息
* @param req
* @param t 错误信息
* @param site 出错的个人空间
* @return
* <h2>Request
Headers</h2>
*/
@SuppressWarnings("rawtypes")
public static String getErrorHtml(HttpServletRequest req,
Throwable t) {
StringBuilder html = new StringBuilder(error_css);
if(req != null){
html.append("<h2>Request
Headers</h2><table>");
html.append("<tr><th>Request
URL</th><td>");
html.append(req.getRequestURL().toString());
if(req.getQueryString()!=null){
html.append('?');
html.append(req.getQueryString());
}
html.append("</td></tr>");
html.append("<tr><th>Remote
Addr</th><td>");
html.append(RequestUtils.getRemoteAddr(req));
html.append("</td></tr>");
html.append("<tr><th>Request
Method</th><td>");
html.append(req.getMethod());
html.append("</td></tr>");
html.append("<tr><th>CharacterEncoding</th><td>");
html.append(req.getCharacterEncoding());
html.append("</td></tr>");
html.append("<tr><th>Request
Locale</th><td>");
html.append(req.getLocale());
html.append("</td></tr>");
html.append("<tr><th>Content
Type</th><td>");
html.append(req.getContentType());
html.append("</td></tr>");
Enumeration headers =
req.getHeaderNames();
while(headers.hasMoreElements()){
String key =
(String)headers.nextElement();
html.append("<tr><th>");
html.append(key);
html.append("</th><td>");
html.append(req.getHeader(key));
html.append("</td></tr>");
}
html.append("</table><h2>Request
Parameters</h2><table>");
Enumeration params =
req.getParameterNames();
while(params.hasMoreElements()){
String key =
(String)params.nextElement();
html.append("<tr><th>");
html.append(key);
html.append("</th><td>");
html.append(req.getParameter(key));
html.append("</td></tr>");
}
html.append("</table>");
}
html.append("<h2>");
html.append(t.getClass().getName());
html.append('(');
html.append(DateFormatUtils.format(System.currentTimeMillis(),
"yyyy-MM-dd HH:mm:ss"));
html.append(")</h2><pre>");
try {
html.append(_Exception(t));
} catch (IOException ex) {}
html.append("</pre>");
html.append("<h2>System
Properties</h2><table>");
Set props =
System.getProperties().keySet();
for(Object prop : props){
html.append("<tr><th>");
html.append(prop);
html.append("</th><td>");
html.append(System.getProperty((String)prop));
html.append("</td></tr>");
}
html.append("</table>");
return html.toString();
}
/**
* 将当前上下文发生的异常转为字符串
* @return
* @throws IOException
*/
private static Throwable _GetException(HttpServletRequest req)
{
if(req == null) return null;
Throwable t =
(Throwable)req.getAttribute("javax.servlet.jsp.jspException");
if(t==null){
//Tomcat的错误处理方式
t = (Throwable)req.getAttribute("javax.servlet.error.exception");
}
return t;
}
/**
* 将异常信息转化成字符串
* @param t
* @return
* @throws IOException
*/
private static String _Exception(Throwable t) throws IOException{
if(t == null)
return null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try{
t.printStackTrace(new PrintStream(baos));
}finally{
baos.close();
}
return baos.toString();
}