//*******************The Log class
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.uitl.Date;
import java.text.DateFormat;
public class Log{
private static final String filePath = PropertyReader.getResource("Log_File_Path");//Supposing we have define in the last ProperyReader class and the file
public static final String EXCEPTION = "Exception";
public static final String CREATE_STAFF = "Create Staff";
public static final String EDIT_STAFF = "Edit Staff";
public static final String DELETE_STAFF = "Delete Staff";
public static final String RECORD_HAS_EXIST = "Record Has Exist";
public static void log(String msg_type, Exception e){
StringBuffer errMsg = new StringBuffer(e.toString);
for(int i=0;i<e.getStackTrace().length;i++){
errMsg.append("\n\t at");
errMsg.append(e.getStackTrace()[i].toString);
}
log(msg_type,errMsg.toString());
OptionPanel.showErrMsg("Sorry,System may have an error \n System will exit");
System.exit(-1);
}
public static void log(String msg.type,Staff staff){
String msg = null;
if(msg_type == CREATE_STAFF){
msg = staff.toString() + "has benn created";
}else if(msg_type == EDIT_STAFF){
msg = staff.toString() + "has been Changed";
}else if(msg_type == DELETE_STAFF){
msg = staff.toString() + "has been Deleted";
}else if(msg_type == RECORD_HAS_EXIST){
msg = staff.toString() + "has exist in the database";
}
log(msg_type,msg);
}
private static void log(String msg_type,String msg){
BufferedWriter out = null;
DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM);
try{
out = new BufferedWriter(new FileWriter(getLogFilePath(),true));//如果为 true,则将字节写入文件末尾处,而不是写入文件开始处
out.write("["+df.format(new Date()) + "] <" + msg_type + "> :" + msg);
out.newline();
out.newline();
}catch(IOException e){
e.printStackTrace();
}finally{
try{
if(out!=null){
out.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
private static String getLogFilePath(){
File logDir = new File(filePath);
if(!logDir.exists()){
logDir.mkdir();
}
int i = 1;
String fileName = filePath + "log_";
File file = new File(fileName + i + ".txt");
while(file.exists() && file.length() > 30000L) {
i++;
file = new File(fileName + i + ".txt");
}
return fileName + i + ".txt"
}
}
//*****************************The OptionPanel Dialog Class for the Log Class
import javax.swing.JOptionPane;
public class OptionPanel {
private static final String appTitle = PropertyReader.getResource("App_Title");//suposing the file has been established and the property app-title stands for the name of application
private static final MainFrame frame = MainFrame.getMainFrame();
public static void showWarningMsg(String msg){
JOptionPane.showMessageDialog(frame,msg,appTitle,JOptionPane.WARNING_MESSAGE);
}
public static void showErrMsg(String msg){
JOptionPane.showMessageDialog(frame,msg,appTitle,JOptionPane.Error_MESSAGE);
}
public static int showConfirmMsg(String msg){
return JOptionPane.showConfirmDialog(frame,msg,appTitle,JOptionPane.YES_NO_OPTON,JOptionPane.QUESTION_MESSAGE);
}
}
posted on 2007-04-05 10:01
wqwqwqwqwq 阅读(865)
评论(1) 编辑 收藏 所属分类:
Simple Java