2014-09-21 32 views
-1

通過下面的代碼,我試圖模擬命令shell,我甚至創建了一個命令並將其命名爲(Showerrlog),以幫助用戶查看他在當前工作期間輸入的無效命令會話,就像你看到的那樣,我使用filehandler來完成這項工作,它將在日誌文件中保存錯誤的命令。但正如你所知,filehandler會爲每個新的工作會話啓動一個新文件,新文件將被命名爲(file.log,file.log.1,file.log.2等),問題是:如何使程序避免每次都打開一個新文件,換句話說,沒有其他方法可以讓程序只格式化上一個工作會話並添加新的文件?將錯誤保存到文件處理程序

或者至少如何讓程序打開屬於當前工作會話的最後一個日誌文件?

public class WithEyul implements Runnable { 

    String command; 

    public WithEyul(String command) { 
     this.command = command; 
    } 

    @Override  
    public void run() { 
     List<String> input = new ArrayList<String>(); 
     StringTokenizer tokenizer = new StringTokenizer(command); 
     while (tokenizer.hasMoreTokens()) { 
      input.add(tokenizer.nextToken()); 
     } 
     ProcessBuilder pb = new ProcessBuilder(input); 
     // ProcessBuilder creates a process corresponding to the input command 
     // now start the process 
     BufferedReader br = null; 
     try { 
      Process proc = pb.start(); 
      // obtain the input and output streams 
      InputStream is = proc.getInputStream(); 
      InputStreamReader isr = new InputStreamReader(is); 
      br = new BufferedReader(isr); 
      // read what the process returned 
      String line; 
      while ((line = br.readLine()) != null) { 
       System.out.println(line); 
      } 
      br.close(); 
     } catch (java.io.IOException ioe) { 

      try { 
       System.err.println("Error"); 

       Logger logger = Logger.getLogger("Testing"); 
       FileHandler fh = new FileHandler("E:/MyLogFile.log"); 

       logger.addHandler(fh); 
       SimpleFormatter formatter = new SimpleFormatter(); 
       fh.setFormatter(formatter); 
       logger.info(command); 
      } catch (SecurityException e) { 
       printStackTrace(); 
      } catch (IOException ex) { 
       Logger.getLogger(WithEyul.class.getName()).log(Level.SEVERE, null, ex); 
      } 
     } finally { 
      if (br != null) { 
       try { 
        br.close(); 
       } catch (IOException ex) { 
        Logger.getLogger(WithEyul.class.getName()).log(Level.SEVERE, null, ex); 
       } 
      }  
     }  
    }  
} 

,這裏是主要的方法類

public class TestProcessBuilder { 

    static void createProcess(String command) throws java.io.IOException { 
     Thread t = new Thread(new WithEyul(command)); 
     t.start();  
    } 

    public static void main(String[] args) throws java.io.IOException { 

     String commandLine; 
     File wd; 
     BufferedReader console = new BufferedReader(new InputStreamReader(System.in)); 
     System.out.println("\n\n***** Welcome to the Java Command Shell *****"); 
     System.out.println("If you want to exit the shell, type END and press RETURN.\n"); 
     // we break out with ‘END’ 
     while (true) { 
      // show the Java shell prompt and read what command they entered 
      System.out.print("jsh>"); 
      commandLine = console.readLine(); 
      // if user entered a return, just loop again 
      if (commandLine.equals("")) { 
       continue; 
      } 
      if (commandLine.equalsIgnoreCase("Showerrlog")) { 
       try { 

        // Runtime.getRuntime().exec("E:\\MyLogFile.log"); 
        if (Desktop.isDesktopSupported()) { 
         Desktop.getDesktop().open(new File("E:\\MyLogFile.log")); 
        } 

       } catch (IOException ex) { 
        Logger.getLogger(WithEyul.class.getName()).log(Level.SEVERE, null, ex); 
       } 
      } 
      if (commandLine.toLowerCase().equals("end")) { //User wants to end shell 
       System.out.println("\n***** Command Shell Terminated. See you next time. BYE for now. *****\n"); 
       System.exit(0); 
      } 

      createProcess(commandLine); 

     } 
    } 
} 

回答

0

您可以使用FileHandler構造函數,它允許你指定旋轉,限制和附加選項。

new FileHandler("E:/MyLogFile.log", 0, 1, true); 

FileHandler可以旋轉的原因有很多是你無法控制的。如果你不想處理文件旋轉,你可以打開一個FileOutputStream並用StreamHandler來包裝它。但是,您將不得不處理文件鎖定衝突。

您還應該避免創建並添加處理程序,該處理程序在每次生成錯誤時指向同一個目標文件。您應該在啓動時安裝處理程序並存儲string reference to your logger