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();
}
}
}
相关文章
SpringSecurity+OAuth2.0 搭建认证中心和资源服务中心流程分析
OAuth 2.0 主要用于在互联网上安全地委托授权,广泛应用于身份验证和授权场景,这篇文章介绍SpringSecurity+OAuth2.0 搭建认证中心和资源服务中心,感兴趣的朋友一起看看吧2024-01-01
最新评论