java捕獲異常信息存入txt文件示例
捕獲程序中出現(xiàn)的異常 可用于后期維護的必要性!做簡單的測試 !
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();
}
}
}
相關(guān)文章
詳解Spring Security 中的四種權(quán)限控制方式
這篇文章主要介紹了詳解Spring Security 中的四種權(quán)限控制方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10SpringSecurity+OAuth2.0?搭建認(rèn)證中心和資源服務(wù)中心流程分析
OAuth?2.0?主要用于在互聯(lián)網(wǎng)上安全地委托授權(quán),廣泛應(yīng)用于身份驗證和授權(quán)場景,這篇文章介紹SpringSecurity+OAuth2.0?搭建認(rèn)證中心和資源服務(wù)中心,感興趣的朋友一起看看吧2024-01-01詳解SpringBoot如何創(chuàng)建自定義Starter
Spring Boot的自動配置機制為開發(fā)人員提供了一種輕松集成和配置各種功能的便捷方式,本文將深入探討在Spring Boot中如何創(chuàng)建自定義Starter,為構(gòu)建模塊化且易維護的應(yīng)用提供有力的支持,需要的朋友可以參考下2024-02-02Java程序圖形用戶界面設(shè)計之標(biāo)簽組件
圖形界面(簡稱GUI)是指采用圖形方式顯示的計算機操作用戶界面。與早期計算機使用的命令行界面相比,圖形界面對于用戶來說在視覺上更易于接受,本篇精講Java語言中關(guān)于圖形用戶界面的標(biāo)簽組件部分2022-02-02