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