Write a Log Parser | Generic | Calculating processing time [Performance]

Hello All,


below is the program in java to parse a log :

/*##############################      Program Name: Log parser                                                                                        ####################################      Craete Date: 27-March-2018                                                                                      ####################################      Auther: Surender Sharma                                                                                         ####################################      Description: Parse the Adpater and transformation log to find the time taken to process messages / files        ######*/
package LogParsers;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import java.io.*;
import java.util.Hashtable;
import java.util.Scanner;
import java.util.regex.Pattern;

public class LogParser_Cash_Adapter   {


        private static String Folder_path ="C:\\TEMP\\code\\"; // this is for 569
        private static int i =0;
        private static int j =0;
        private static int k =0;
        private static int l =0;
        private static Hashtable<String, String> details;


        public static void main(String args[]) {

//to anaylze a log we need The log name, Its End line [i.e. when to say processing of message/file is complete ] , Its Message Line , its start line [i.e. reference text which states this is starting ine of file or message processing],the Text of line in which we will find the name of the file        //analyze_log("ReceiverMT544_545_546_547_Default.log","Created output file :","Received e_instatmsg");        //analyze_log("ReceiverMT940_950_942_910_Default.log","Created output file :","Received swiftmsg");        //analyze_log("ROSETTARAW.log","successful. Moving","Begin processing- ROSETTARAW","File processing successful. Moving");        //analyze_log("SGL.log.2018-03-21","File successfully moved.","Begin processing- SGL","Writing correct file:");        //analyze_log("SGL.log.2018-03-26","File successfully moved.","Begin processing- SGL","Writing correct file:");        //analyze_log("ReceiverMT940_950_942_910_Default.log","Created output file :");        analyze_log("Receiver950-608_Default.log","Added msg with id","Received swiftmsg","Created output file :");
        

    }


    static void analyze_log(String LogName, String EndMsgine,String startMsgline,String FileMsgLine)
    {
        //Setting all the counters to zero        i=j=k=l=0;

        // Create WorkBook        HSSFWorkbook workbook = new HSSFWorkbook();

        // Create a blank sheet        HSSFSheet sheet = workbook.createSheet("Data");

        //Set Scan to null so that for every new log, object is refreshed, try catch is required to verify is file really exist        Scanner scan = null;
        try {
            // Scan the data of the file into object            scan = new Scanner(new File(Folder_path+LogName));


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        // Delimate the whole data into different array spaces        //2018 is used here because we need all the line which has time stamp, don't want to read lines separately when error line is found        //another reason is the content of a line in continuation might have new line char, in that case data will seperate although we needed in single line.        scan.useDelimiter(Pattern.compile("\n2018"));
        // Create Row r with ith value. we have to create separate r row object because if we use directly in setCellvalue line, it will only write conent of last line in this case start date
        Row r = sheet.createRow(i);
        //Set Header Value in each column        r.createCell(0).setCellValue("EndTime");
        r.createCell(1).setCellValue("File");
        r.createCell(2).setCellValue("StartTime");
        //Set the StartLine and filelocname data to blank        String startLineData = null;
        String fileLocname = null;
        // iterate untill all the array segregated as part of scanner is completed        while (scan.hasNext()) {
            //Create a String Name logical line ,which will contain segregated data            String logicalLine= new String();
            //read the line and store            logicalLine=scan.next() ;
            //replace all the /r /n with blank            logicalLine=logicalLine.replaceAll("\r","").replaceAll("\n","");
            // System.out.println(logicalLine);//check if it contains info value            if (logicalLine.contains("INFO"))
            {
 // check if it conatin Startmsgline send in parameter                    if (logicalLine.contains(startMsgline)) {
                        // check if 2018 text is there because first line many contain full date.                                if (logicalLine.contains("2018"))
                                {
                                    // if                                    startLineData = logicalLine.substring(0, 19);
                                }
                            // after segregating the whole text by \n2018 text  ,"value left is -MM-YYYY HH:MM:SS,000" hence 2018 is required to be added and substing is shorten by 4 chars                                else
                                {
                                    startLineData = "2018" + logicalLine.substring(0, 15);
                                }
                    }
//check if it contains Line which contains the Location and file name                    if(logicalLine.contains(FileMsgLine)) {
// if yes save the name and location                        fileLocname = logicalLine.split(FileMsgLine)[1];

                    }
//check if contain EndmsgLine, which will help us getting the end time of that processing                if (logicalLine.contains(EndMsgine))
                                {
                                    // increment the Row number                                    i = i + 1;
                                    //Create new row                                    Row r1 = sheet.createRow(i);
                                    //save the details in respective columns                                    r1.createCell(0).setCellValue("2018"+logicalLine.substring(0, 15));
                                    r1.createCell(1).setCellValue(fileLocname);
                                    r1.createCell(2).setCellValue(startLineData);
                                   // counter for correctparsedMessage                                }
                                // if the line does not contain end msgline                        else                        {
                            // increment the info but not related message counter                            l=l+1;
                        }
            }
            // if  it was not info line            else            {
                //  System.out.println(logicalLine);                //TODO error Counter                k=k+1; // counter for error messages
            }
// increment the counter for no of line            j=j+1; //counter for total messages        }

        System.out.println("Total Lines"+j+ " CorrectMessagesProcessed:"+i +" Errors: "+k +" NotInfo"+l);

        try {
            // this Writes the workbook            FileOutputStream out = new FileOutputStream(new File(Folder_path+LogName+".xls"));
            //write data            workbook.write(out);
            //close the excel            out.close();

            System.out.println(LogName + ".xls written successfully on disk.");
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    }

Comments

Popular posts from this blog

Command Line tool for Android and iOS [ adb and cfgutil ]

Convert Chrome recording to Vugen Script

Jmeter-CheatSheet