我正在将所有异常日志写入文本文件中。
例如
pw= new PrintWriter(new FileWriter("EXCEPTION.txt", true));
try
{
do something!!
} catch (Exception e) {
e.printStackTrace(pw);pw.flush();
}但是,当存在多个异常时,很难读取EXCEPTION.txt文件。我认为最好在每一个例外面前都有时间。
这是如何做到的呢?
发布于 2013-11-05 05:43:50
您只需在将堆栈跟踪追加到文件之前添加当前日期即可。此外,您还可以格式化日期并编写它。
catch (Exception e) {
pw.write(new Date().toString()); // Adding the date
pw.write(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); // Formatted date
e.printStackTrace(pw);
pw.flush();
}发布于 2013-11-05 05:43:02
你可以这样做:
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
String timeNow = dateFormat.format(date); // pretty date
pw.write(timeNow + " " + e.getMessage()); // date concatenated with exception messagehttps://stackoverflow.com/questions/19782578
复制相似问题