📄 debug.java
字号:
package hospital.db;
import java.io.*;
import java.util.Date;
import java.text.SimpleDateFormat;
/**
* 本类用于将网站运行时遇到的异常信息记录到文件中
*
* 作者:Fido Dido
*/
public final class Debug{
private static Debug instance=null;
private static SimpleDateFormat dateFormat=null;
private static FileOutputStream fos=null;
private Debug(){
}
/**
* 初始化Debug对象
*
* 参数:
* path-日志文件存储路径
*
* 返回值-Debug单例对象
*/
static synchronized Debug init(String path){
String file="";
String fullPath="";
if(instance == null){
instance=new Debug();
dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
file="" + (new SimpleDateFormat("yyyy-MM-dd"))
.format(new Date()) + ".log";
try{
fullPath=path + "\\" + file;
fos=new FileOutputStream(fullPath,true);
}
catch(IOException ioe){
System.err.println("Cannot open file " + file + "," + fullPath);
}
}
return instance;
}
/**
* 将信息记入日志文件
*
* 参数:
* msg-信息
*/
public static synchronized void log(String msg){
String s2=dateFormat.format(new Date()) + " " + msg + "\n";
if(instance != null)
instance.writeFile(s2);
else
System.err.println(s2);
}
/**
* 将信息记入日志文件,供log(String msg)调用
*/
private String writeFile(String msg){
if(fos == null){
return "Log file cannot be opened";
}
try{
fos.write(msg.getBytes());
fos.flush();
}
catch(IOException ex){
return ex.getMessage();
}
return null;
}
/**
* 生成格式化异常信息
*
* 参数:
* e-异常
*
* 返回值-格式化后的异常信息
*/
public static String getExceptionMsg(Exception e){
StackTraceElement ste=e.getStackTrace()[0];
String msg=ste.getClassName() + "." + ste.getMethodName() + "() Ln " + ste.getLineNumber() + ": " + e.getMessage();
return msg;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -