posts - 97,  comments - 93,  trackbacks - 0

          //*******************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,則將字節(jié)寫入文件末尾處,而不是寫入文件開(kāi)始處
                  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 閱讀(877) 評(píng)論(1)  編輯  收藏 所屬分類: Simple Java

          FeedBack:
          # re: Write a Log && Use the Properties
          2007-04-07 18:29 | 曲強(qiáng)
          我也叫曲強(qiáng),我QQ139876554,有時(shí)間加我吧  回復(fù)  更多評(píng)論
            
          <2007年4月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345




          常用鏈接

          留言簿(10)

          隨筆分類(95)

          隨筆檔案(97)

          文章檔案(10)

          相冊(cè)

          J2ME技術(shù)網(wǎng)站

          java技術(shù)相關(guān)

          mess

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          校園夢(mèng)網(wǎng)網(wǎng)絡(luò)電話,中國(guó)最優(yōu)秀的網(wǎng)絡(luò)電話
          主站蜘蛛池模板: 托里县| 全南县| 沁阳市| 天峻县| 奉贤区| 吉隆县| 河西区| 新巴尔虎右旗| 施秉县| 尼玛县| 中宁县| 清河县| 乌恰县| 伊吾县| 米易县| 洪泽县| 和林格尔县| 琼海市| 西平县| 浮山县| 沐川县| 沛县| 昌宁县| 黄平县| 盘山县| 辰溪县| 大城县| 永仁县| 睢宁县| 黔西县| 军事| 朝阳县| 宁阳县| 沐川县| 玛曲县| 秀山| 左云县| 隆化县| 高清| 大城县| 通道|