Programming and So

Tips and tricks in Java

Posts Tagged ‘log4j

Getting rolled over log4j files from FTP

without comments

Scenario

  • Log4j configured to roll over log files by size
  • Local FTP process to get all the files relating one fixed date

Error example

  1. Server returns files log.1 and log.2 as modified on target date
  2. Local FTP process starts to download log.1
  3. Server perform a roll over log files renaming log.1 to log.2 and log.2 to log.3
  4. Local FTP process ends log.1 downloading
  5. Local FTP process starts to download log.2 (!!!)

Code pattern for expected result

  [...]
    // Downloaded files
    boolean fileDownloaded;
    ArrayList<String> downloadedFiles = new ArrayList<String>();

    do {

      fileDownloaded = false;

      // Log directory
      client.cd(logDir);
      List<?> ls = client.ls();
      ArrayList<String> filesToDownload = new ArrayList<String>();
      for (Object file : ls) {
        SftpFile sftpFile = (SftpFile) file;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(sftpFile.getAttributes()
            .getModifiedTime().longValue() * 1000L));
        // Target date
        if (calendar.get(Calendar.DAY_OF_MONTH) == dayOfMonth
            && calendar.get(Calendar.MONTH) == (month - 1)
            && calendar.get(Calendar.YEAR) == year) {
          // Mark to download
          filesToDownload.add(sftpFile.getFilename());
        }
      }

      // Update list
      downloadedFiles = updateDownloadedList(downloadedFiles, filesToDownload);

      // Download file
      for (String fileName : filesToDownload) {
        if (!downloadedFiles.contains(fileName)) {
          client.get(fileName, currentDir + "/" + fileName);
          downloadedFiles.add(fileName);
          fileDownloaded = true;
          break;
        }
      }

    } while (fileDownloaded);

[...]

/**
 * Update downloaded log4j files renaming numeric log extension
 * @param downloadedFiles currently downloaded files (local machine)
 * @param availableFiles currently listed files (log4j server)
 * @return
 */
private ArrayList<String> updateDownloadedList(ArrayList<String> downloadedFiles, ArrayList<String> availableFiles) {
 
  ArrayList<String> updatedDownloadedFiles = new ArrayList();
 
  for (String downloadedFile : downloadedFiles) {
   
    if (!availableFiles.contains(downloadedFile)) {
     
      do {
          if (downloadedFile.endsWith(".log")) {

            downloadedFile = downloadedFile + ".1";

          } else {

            int lastDotPosition = downloadedFile.lastIndexOf(".") + 1;
            int nextRollOverNum = Integer.parseInt(downloadedFile.substring(lastDotPosition, downloadedFile.length())) + 1;
            downloadedFile = downloadedFile.substring(0, lastDotPosition) + nextRollOverNum;

          }
      } while (!availableFiles.contains(downloadedFile));
     
      updatedDownloadedFiles.add(downloadedFile);
     
    } else {
     
      updatedDownloadedFiles.add(downloadedFile);
     
    }
  }
 
  return updatedDownloadedFiles;
}

Written by angelborroy

August 5, 2008 at 1:05 pm

Posted in java

Tagged with ,