如何将异常堆栈跟踪打印到网页?
import com.opensymphony.xwork2.ActionSupport;
import java.util.Arrays;
public class ExceptionAction extends ActionSupport
{
public String execute()
{
//
String errMsg = Arrays.toString(Thread.currentThread().getStackTrace());
this.addActionError(
"Unexcepted error has occurred. \n"
+errMsg
+ "\nPlease try again or report this error to administrator or support team. \n ");
return "error";
}
/**
*
*/
private static final long serialVersionUID = -3754090784536811052L;
}
但是我得到了上面代码无用的味精,味精是这样的:
[java.lang.Thread.getStackTrace(Thread.java:1479), com.monitoring.action.ExceptionAction.execute(ExceptionAction.java:11), sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method), sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39),
最佳答案
File file = new File("test.log");
PrintStream ps = new PrintStream(file);
try {
// something
} catch (Exception ex) {
ex.printStackTrace(ps);
}
ps.close();
另见Difference between printStackTrace() and toString()
关于java - 如何将堆栈跟踪打印到页面?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17248310/