java捕获异常信息存入txt文件示例
捕获程序中出现的异常 可用于后期维护的必要性!做简单的测试 !
package helpEntity;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Log {
private File file = null;
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public void saveLog(Exception e, String youName) {
try {
String nowPath = null;
nowPath = System.getProperty("user.dir");
String tempPath = null;
this.file = new File(nowPath);
tempPath = this.file.getParent();
if (tempPath == null) {
this.file = new File(nowPath);
}
this.file = new File(tempPath + "" + File.separator + "log.txt");
PrintWriter writer = null;
FileWriter fileWrite = new FileWriter(file, true);
writer = new PrintWriter(fileWrite);
writer.append(System.getProperty("line.separator")
+ new SimpleDateFormat("yyyy-MM-dd:HH:mm:ss")
.format(new Date()) + "__" + youName);
writer.append(System.getProperty("line.separator"));
writer.append(" *************************" + e.toString()
+ "*************************");
writer.append(System.getProperty("line.separator"));
e.printStackTrace(writer);
writer.flush();
writer.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
相关文章
Java线程安全解决方案(synchronized,ReentrantLock,Atomic)
这篇文章主要介绍了Java线程安全解决方案(synchronized,ReentrantLock,Atomic),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-09-09
使用Spring-Retry解决Spring Boot应用程序中的重试问题
重试的使用场景比较多,比如调用远程服务时,由于网络或者服务端响应慢导致调用超时,此时可以多重试几次。用定时任务也可以实现重试的效果,但比较麻烦,用Spring Retry的话一个注解搞定所有,感兴趣的可以了解一下2023-04-04


最新评论