Web Applications-Deployment-Logging

Version 3.1 by Pascal Robert on 2007/09/03 21:48

Log Rotation

Mike Kienenberger

I can't remember who gave me the basic starting point for this, but I think it may have been one of the Jonathans. You'll need to change the filenames to make it work under non-unix deployment systems.

You'll find it easier to copy out the source by going into Edit mode first.


   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);
   }