0

Im trying to print out log entries between two date ranges but am having some trouble doing so.

I have read the entries from the file and stored got the info i want as date varibles but i still can figure out why it wont display the dates in range

Here is the file

 2012-09-13 16:04:22 DEBUG SID:34523 BID:1329 RID:65d33 'Starting new session'
 2012-09-13 16:04:30 DEBUG SID:34523 BID:1329 RID:54f22 'Authenticating User'
 2012-09-13 16:05:30 DEBUG SID:42111 BID:319 RID:65a23 'Starting new session'
 2012-09-13 16:04:50 ERROR SID:34523 BID:1329 RID:54ff3 'Missing Authentication token'
 2012-09-13 16:05:31 DEBUG SID:42111 BID:319 RID:86472 'Authenticating User'
 2012-09-13 16:05:31 DEBUG SID:42111 BID:319 RID:7a323 'Deleting asset with ID 543234'
 2012-09-13 16:05:32 WARN SID:42111 BID:319 RID:7a323 'Invalid asset ID'
 2012-09-14 16:04:22 DEBUG SID:34523 BID:1329 RID:65d33 'Starting new session'
 2012-09-14 16:04:30 DEBUG SID:34523 BID:1329 RID:54f22 'Authenticating User'
 2012-09-14 16:05:30 DEBUG SID:42111 BID:319 RID:65a23 'Starting new session'
 2012-09-14 16:04:50 ERROR SID:34523 BID:1329 RID:54ff3 'Missing Authentication token'
 2012-09-14 16:05:31 DEBUG SID:42111 BID:319 RID:86472 'Authenticating User'
 2012-09-14 16:05:31 DEBUG SID:42111 BID:319 RID:7a323 'Deleting asset with ID 543234'
 2012-09-14 16:05:32 WARN SID:42111 BID:319 RID:7a323 'Invalid asset ID'

Here is my code

import java.io.File;
import java.io.FileNotFoundException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;
import java.util.Scanner;

public class ReadLogs {
private static String line, logLevel = "16:05:31", businessID ="1329", sessionID ="34523",
        startDateStr = "2012-09-10",endDateStr ="2012-09-14" ;
private static String logString ="";
private static ArrayList<String> logList = new ArrayList<>();

public static void main(String args[]){
    readFile();
    returnLogLinesByLevel();
    returnLogLinesByBusiness();
    returnLogLinesBySessionID();
    returnLogLinesByDate();
}//close main

/*
 * This method checks the logs for entries between a date range. 
 * @param String dateToValidate - This is the date to search the logs for
 * @param String startDate - This is the starting date point
 * @param String endDate - This is the end date point
 * */
private static void returnLogLinesByDate() {
    System.out.println("///////By Date/////////////////");
   for( int i = 0; i <= logList.size() - 1; i++)
      {
           logString = logList.get(i);
          if(isThisDateWithinRange(logString.substring(0, 11))){
              System.out.println(logString);
          }
      }
   System.out.println("/////////////////////////////////");
}

/*
 * This method checks the logs for entries by the Session ID
 * @param String sessionID - this is the session ID to search for
 * */
private static void returnLogLinesBySessionID() {
    System.out.println("///////By Session ID//////////////");
    checkLogs(sessionID);
    System.out.println("//////////////////////////////////");
}
/*
 * This method checks the logs for entries by the business ID 
 * @param String businessID - this is the business ID to search for
 * */
private static void returnLogLinesByBusiness() {
    System.out.println("///////By Business ID/////////////");
    checkLogs(businessID);
    System.out.println("//////////////////////////////////");
}
/*
 * This method checks the logs for entries by the log level
 * @param String logLevel - this is the log level to search for
 * */
private static void returnLogLinesByLevel() {
    System.out.println("///////By Level/////////////");
    checkLogs(logLevel);
    System.out.println("////////////////////////////");     
   }

/* 
 * This method checks if the logs contain a string of characters
 * @param String logContentToSearchFor - The String to search logs for
 * */
private static void checkLogs(String logContentToSearchFor){
    for( int i = 0; i <= logList.size() - 1; i++)
    {
        logString = logList.get(i);
        if(logString.contains(logContentToSearchFor)){System.out.println(logString);}   
    }
}

/* 
 * This method checks whether the 'dateToValidate' is inside the specified start/end dates
 * and returns true/false based on the outcome
 * */
public static boolean isThisDateWithinRange(String string){
    try
    {

        Date date = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH).parse(string);
        Date startDate =  new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH).parse(startDateStr);
        Date endDate =  new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH).parse(endDateStr);
        if(date.after(startDate) &&  date.before(endDate)){
            return true;
        } else{
            return false;
        }
    }catch(ParseException e) {e.printStackTrace();
            return false;
        }
}

private static void readFile() {
    /*Read log file*/
    Scanner logScanner = null;
    try {
        logScanner = new Scanner(new File("C:\\Users\\"+System.getProperty("user.name")+"\\Desktop\\logs.txt"));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    while (logScanner.hasNextLine()) {
        line = logScanner.nextLine();
        logList.add(line);                  
    }   
}

}

CoffeeTime
  • 305
  • 1
  • 6
  • 17
  • What do you mean by range? Can you give one example of output that you expect to see? – Mehmet Sedat Güngör Apr 14 '14 at 22:03
  • id expect to see the following 2012-09-13 16:04:22 DEBUG SID:34523 BID:1329 RID:65d33 'Starting new session' 2012-09-13 16:04:30 DEBUG SID:34523 BID:1329 RID:54f22 'Authenticating User' 2012-09-13 16:05:30 DEBUG SID:42111 BID:319 RID:65a23 'Starting new session' 2012-09-13 16:04:50 ERROR SID:34523 BID:1329 RID:54ff3 'Missing Authentication token' 2012-09-13 16:05:31 DEBUG SID:42111 BID:319 RID:86472 'Authenticating User' 2012-09-13 16:05:31 DEBUG SID:42111 BID:319 RID:7a323 'Deleting asset with ID 543234' 2012-09-13 16:05:32 WARN SID:42111 BID:319 RID:7a323 'Inv ID'@MehmetSedatGüngör – CoffeeTime Apr 14 '14 at 22:08
  • 1
    @CoffeeTime What is the difference between this and your [other question](http://stackoverflow.com/questions/23065301/how-to-print-out-a-string-between-certain-dates-from-log-file)? – CodeCamper Apr 14 '14 at 22:09

3 Answers3

0

The conditon to check the range is incorrect, it should be :

  if(date.after(startDate) &&  date.before(endDate)){
      // valid date
  }

Your previous condition will return true if the date is less than startDate or if its greater than endDate which is the opposite of what you want.

Kakarot
  • 4,252
  • 2
  • 16
  • 18
  • Hi, It still doesnt work. All the contents of the file are output. @Kakarot – CoffeeTime Apr 14 '14 at 22:30
  • @CoffeeTime from the data that you provided in the question it seems all the dates fall in this range – Kakarot Apr 14 '14 at 22:35
  • I cant modify the data so how would i filter for only log entries on 2012-09-13 – CoffeeTime Apr 14 '14 at 22:40
  • try this if(date.getTime() == startDate.getTime()) {} in this startDate = "2012-09-13" – Kakarot Apr 14 '14 at 22:45
  • where are you setting dateToValidate variable, I cannot see that in the code – Kakarot Apr 15 '14 at 00:53
  • If you scroll across the dateToValidate variable is set to 2012-09-13.. I think the problem is with the SimpleDateFormat and parser – CoffeeTime Apr 15 '14 at 09:01
  • Found the problem. I was passing the date to validate as the date to check between the two dates so it always returned true and therefore printed out every log entry. What i needed to do was pass the date for the current log entry – CoffeeTime Apr 15 '14 at 10:04
0

In your method isThisDateWithinRange() you are for checking is a date between two dates use variable dateToValidate which never has changed (For every method call it has value "2012-09-13"). You should change method signature and pass argument date which you want to check is in range. It should be something like this:

public static boolean isThisDateWithinRange(String strDate){
  // ....
  Date date = sdf.parse(strDate);
  // ...
}

And as @Kakarot have answered, you should use:

 if(date.after(startDate) &&  date.before(endDate)){
      // valid date
  }

If you use logic OR (||) date is always after the start date OR before the end date, and in that case method, isThisDateWithinRange will always return true.

djm.im
  • 3,295
  • 4
  • 30
  • 45
0

I was passing the date to validate as the date to check between the two dates so it always returned true and therefore printed out every log entry. What i needed to do was pass the date for the current log entry.

if(isThisDateWithinRange(logString.substring(0, 11)))
CoffeeTime
  • 305
  • 1
  • 6
  • 17