跳转到内容

WebObjects/Web 应用程序/部署/日志记录

来自维基教科书,开放的书籍,开放的世界

日志轮换

[编辑 | 编辑源代码]

迈克·基南伯格

[编辑 | 编辑源代码]

我不记得是谁给了我这个基本起点,但我认为可能是乔纳森之一。您需要更改文件名以使其在非 Unix 部署系统下工作。

您会发现先进入编辑模式更容易复制源代码。

   public static void main(String argv[])
   {
       try
       {
           String baseOutputPath = null;
           
           //look for a -WOOutputPath argument
           for (int i=0; i<argv.length; i++) {
               if (  argv[i].equals("-WOOutputPath") &&
                   !argv[i+1].equals("/dev/null")  ) {
                   String outputPath = argv[i+1];
                   baseOutputPath = outputPath;
                   java.io.File outputFile = new java.io.File(outputPath);
                   if (outputFile.exists()) {
                       // move old file out of way to new location,
                       //name based on existing name with an appended timestamp
 
                       // Format the current time.
                       java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");
                       java.util.GregorianCalendar now = new java.util.GregorianCalendar();
                       String dateSuffix = ".bck-" + formatter.format(now.getTime());
                       System.err.println("new name: " + outputPath + dateSuffix);
                       java.io.File renamedFile = new java.io.File(outputPath + dateSuffix);
                       outputFile.renameTo(renamedFile);
                   }
                   break;
               }
           }
       }
       catch (Throwable e) {
           //just so any Throwables generated in trying to do this don't
           //keep our app from launching.
           System.err.println("Ignoring: " + e);
       }
 
       WOApplication.main(argv, Application.class);
   }
华夏公益教科书