<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 使用 Java 在 Linux 中管理不超過 N GB 的系統日志文件 > 原文: [https://howtodoinjava.com/linux/manage-system-log-files-in-linux-using-java/](https://howtodoinjava.com/linux/manage-system-log-files-in-linux-using-java/) 如今,大多數應用程序都需要管理自己的日志文件,包括在達到特定大小限制時將其刪除。 在本文中,我將嘗試為這種日志文件管理提出一種解決方案。 我還將建議捕獲 [*linux*](https://en.wikipedia.org/wiki/Linux "Linux") 進程的輸出并將其記錄到一些單獨的日志文件中的方法。 本文是我上一篇文章的延續:[自動重載配置](//howtodoinjava.com/java-7/auto-reload-of-configuration-when-any-change-happen/ "Auto reload of configuration when any change?happen"),其中我討論了只要有人在文件系統中更改配置文件,應用程序如何無需重新啟動應用程序即可完成其配置重載的工作 。 **這篇文章中的部分:** * 確定方法 * 編寫 bash 腳本 * 編寫腳本執行器 * 添加食流者 * 查看全部 ## 確定方法 我相信,要把工作做好,我們肯定需要一個 bash 腳本。 Linux 有一些非常有用的內置命令,通過命令窗口執行時,這些命令可以立即執行此工作。 bash 腳本的優勢包括將實際應用程序的文件處理程序代碼解耦,如果需要任何特定于平臺的更改,可以對其進行修改。 ## 編寫 bash 腳本 我已經編寫了一個演示腳本。 您可以根據需要使用它: ```java #!/bin/bash ############################################################################### # # discCleanUp.sh # ################################################################################ DIR_PATH=$1 NUM_OF_DAYS=$2 DIR_SIZE=$3 SIZE=1024 DIR_SIZE=$(($DIR_SIZE * $SIZE * $SIZE)) # Command to find the File older then NUM_OF_DAYS and removing them from directory DIR_PATH find $DIR_PATH -mtime +$NUM_OF_DAYS -exec rm {} ; #Go to specified directory cd $DIR_PATH # Command to get the current directory size. CURR_DIR_SIZE=`du -sk | cut -f1` while [[ $CURR_DIR_SIZE -gt DIR_SIZE ]]; do echo $CURR_DIR_SIZE FILE=`ls -1tra | head -1` rm -rf $FILE # Command to get the current directory size. CURR_DIR_SIZE=`du -sk | cut -f1` done exit 0 ``` 在上面的腳本中,`DIR_PATH`,`NUM_OF_DAYS`和`DIR_SIZE`是命令行參數。 而`SIZE`是用于計算目的的常數。 在上面的腳本中,**行號 16** 將刪除 n 天之前的日志文件。 在**行號 22** 中,腳本使用[*`du`命令*](http://linux.about.com/library/cmd/blcmdl1_du.htm "Linux du command")檢查目錄的當前大小。 如果大小超過`DIR_SIZE`(以 GB 為單位),腳本將開始使用`ls -1tra | head -1`命令并開始將它們一個一個地刪除。 它將繼續直到目錄大小未達到所需的限制。 ## 編寫腳本執行器 至于本文,核心組件將保留在 bash 腳本之上,但仍然需要一種從應用程序運行`.sh`文件的方法。 最好的方法是使用線程(如果需要,可以使用[執行器](https://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/ExecutorService.html "Executor Service"))。 該線程將以可配置的速率定期執行以上 bash 腳本。 我不會寫那部分代碼。 如果執行代碼時遇到任何問題,請寫下注釋。 讓我給你一個示例代碼:: ```java package corejava.dischandler; import java.io.IOException; /** * Title: DiskFileManager.java * Description :- This class provides API to check file size and file time threshold and * if threshold is crossed then deletes file to come within threshold. */ public final class DiskFileManager { private static DiskFileManager diskFileManager=new DiskFileManager(); private DiskFileManager() { } /** * <pre> * <b>checkAndDeleteFiles</b> * <b>Description:This method is called to clean the files from the file system.</b> * </pre> * @throws InterruptedException * @throws IOException * @throws InstantiationException */ public?? void checkAndDeleteFiles() throws InterruptedException, IOException, InstantiationException { try { StringBuffer outStr = new StringBuffer(); StringBuffer errStr = new StringBuffer(); String scriptFileName = "./discfilehandler.sh"; String command = scriptFileName + "/logs 1 1"; System.out.println("Command to be executed? :- " + command); Process output = Runtime.getRuntime().exec(command); StreamEater errorEater = new StreamEater(output.getErrorStream(),errStr); StreamEater outputEater = new StreamEater(output.getInputStream(),outStr); errorEater.start(); outputEater.start(); try { int ret = output.waitFor(); errorEater.join(); outputEater.join(); System.out.println(); //logger.info("execute(): Error Stream:" + errStr + " ; execute(): Output Stream:" + outStr + " ; execute(): ExitValue:" + ret); } catch (InterruptedException e) { throw e; } } catch (IOException e) { throw e; } } /** * <pre> * <b>getInstance</b> * <b>Description:This method is used to get the instance of Disk File Manager.</b> * </pre> * @return */ public static DiskFileManager getInstance() { return diskFileManager; } } ``` 上面的代碼將在類路徑中找到腳本文件,并傳遞所需的參數。 在我們的例子中,`/logs 1 1`是 3 個參數,這意味著 * 要監視的目錄是`/logs`, * 刪除一天的日志文件,然后 * 請勿在任何時候存儲超過 1 GB 的日志文件。 ## 添加`StreamEater` 因此,我們已經添加了一個 bash 腳本和一個用于運行腳本的執行器代碼。 您必須編寫自己的線程才能在執行器上方定期運行。 現在,我們將研究`StreamEater`,它實際上收集命令輸出并提供給您的應用程序代碼,因此應用程序可以記錄它或執行所需的任何操作。 我們已經在上面的執行器中使用過`StreamEater`,現在我給出其代碼: ```java package corejava.dischandler; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; public class StreamEater extends Thread { /** * Stream Eater thread to eat up every thing that is the output to the * execute command */ private InputStream? iStream; public InputStream getiStream() { return iStream; } public void setiStream(InputStream iStream) { this.iStream = iStream; } private StringBuffer stringBuffer; public StringBuffer getStringBuffer() { return stringBuffer; } public void setStringBuffer(StringBuffer stringBuffer) { this.stringBuffer = stringBuffer; } /** * This is the constructor. * * @param is - It is the input stream * @param type - type of input stream * @param redirect - string buffer to contain the output. * @throws InstantiationException */ public StreamEater(InputStream is, StringBuffer redirect) throws InstantiationException { this.iStream = is; if (redirect == null) { throw new InstantiationException("String Buffer Reference , second param can not be null"); } this.stringBuffer = redirect; } /** * This is method called when the thread is started. It captures the stream * output and and store it into the buffer. */ public void run() { InputStreamReader isr = null; BufferedReader br = null; try { isr = new InputStreamReader(iStream); br = new BufferedReader(isr); String line; while ((line = br.readLine()) != null) { stringBuffer.append(line).append(System.getProperty("line.separator")); } } catch (Exception ex) { ex.printStackTrace(); } finally { if (isr != null) { try { isr.close(); } catch (Exception e) { e.printStackTrace(); } } if (br != null) { try { br.close(); } catch (Exception e) { e.printStackTrace(); } } } } } ``` 如您所見,這些都是等待收集進程輸出的小線程。 ## 查看全部 使用上述類和給定的 bash 腳本,您可以輕松開發一些應用程序組件,以確保日志文件的大小不超過某些預定義的數目。 當您的應用程序負責刪除實際的日志文件時,可以在任何級別對??其進行自定義。 希望您在這篇文章中得到一些有用的信息。 如果需要任何幫助或有任何疑問,請給我留言。 學習愉快!
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看