0

生のログファイルダンプを解析し、関連情報を抽出します。

以下は、2つのイベントのログファイルの抽出です。以下に示すように、各イベントは**で区切られます。

01/23/13 17:29:25 |-| *****************************************************************
01/23/13 17:29:25 |-| MTCLPrint: processCodesoftPrintRequest() [-WEB--JU-LBCH-Wed Jan 23 17:29:25 CST 2013]...
01/23/13 17:29:25 |-|   ==> CS Label: [JU , LBCH , 70005023489 , JU Filtrete UPC0 3 Up Label , JU Filtrete UPC0 Labels 3 up , JUCHIZ01 , Qty: 1100]
01/23/13 17:29:26 |-| TrkgNbr: [-WEB--JU-LBCH-Wed Jan 23 17:29:25 CST 2013] ,CSPid: 5372
01/23/13 17:29:27 |-| Sending print job to -\\JUFP01\JUCHIZ01 [-WEB--JU-LBCH-Wed Jan 23 17:29:25 CST 2013]...
01/23/13 17:29:29 |-| Cannot delete file in temp dir (mergeAndPrint) - P786406707_67724_818342796.prn
01/23/13 17:29:29 |-| MTCLPrint: processCodesoftPrintRequest() [-WEB--JU-LBCH-Wed Jan 23 17:29:25 CST 2013]...complete.
01/23/13 17:29:29 |-| ~~~~ MTCL Print Web Service is terminated. ~~~~
01/23/13 17:29:56 |-| ~~~~ MTCL Print Web Service is started. ~~~~
01/23/13 17:29:56 |-| *****************************************************************
01/23/13 17:29:56 |-| MTCLPrint: processCodesoftPrintRequest() [-WEB--SDL-P1-Wed Jan 23 17:29:56 CST 2013]...
01/23/13 17:29:56 |-|   ==> CS Label: [SDL , P1 , 70000437403 , SDL GenericShip.CS7Z170 10-2006 REV , Medina HD Two Part Inter. Label , Z170 Packer , Qty: 1]
01/23/13 17:29:56 |-| TrkgNbr: [-WEB--SDL-P1-Wed Jan 23 17:29:56 CST 2013] ,CSPid: 8840
01/23/13 17:29:58 |-| Sending print job to -\\SPPRT10\SDL-PR-Zebra03 [-WEB--SDL-P1-Wed Jan 23 17:29:56 CST 2013]...
01/23/13 17:29:58 |-| Cannot delete file in temp dir (mergeAndPrint) - P1905794774_98669_986327948.prn
01/23/13 17:29:58 |-| MTCLPrint: processCodesoftPrintRequest() [-WEB--SDL-P1-Wed Jan 23 17:29:56 CST 2013]...complete.
01/23/13 17:29:58 |-| ~~~~ MTCL Print Web Service is terminated. ~~~~
01/23/13 17:30:11 |-| ~~~~ MTCL Print Web Service is started. ~~~~

アプリケーションログによってダンプされる一連の同様の繰り返しログがあります。このファイルを解析し、JavaプログラムによってMSExcelに入力する必要があります。

以下は、上記の生データダンプから取得する必要がある関連事項です。

01/23/13 17:29:25 |-|   ==> CS Label: [JU , LBCH , 70005023489 , JU Filtrete UPC0 3 Up Label , JU Filtrete UPC0 Labels 3 up , JUCHIZ01 , Qty: 1100]

01/23/13 17:29:56 |-|   ==> CS Label: [SDL , P1 , 70000437403 , SDL GenericShip.CS7Z170 10-2006 REV , Medina HD Two Part Inter. Label , Z170 Packer , Qty: 1]

この生データは常に「CSLabel:」で始まり、その後に「[」が続き、コンマで区切られた7つのフィールドがあります。これらの7つのフィールドをExcelシートの列に抽出する必要があります。これを達成するための最も効率的な方法は何でしょうか?

4

2 に答える 2

1

次のアルゴリズムが表示されます。

  1. ファイルを行ごとに読み取ります。
  2. 行にCS Label文字列が含まれている場合は、それを処理します (そうでない場合は破棄します)。
  3. 角かっこの間のすべてを抽出します (これには正規表現を使用するか、単純なString方法を使用できます)。
  4. この文字列をコンマで分割し、スペースをトリムします。
  5. この情報を何らかの構造に追加して、Excel に公開します。
于 2013-02-06T07:47:39.457 に答える
1

どうぞ:

public class DateFunctions {
    // Date related Logic
    private Date startDate;
    private Integer delta;

    @SuppressWarnings("resource")
    public DateFunctions() {
        // ApplicationContext ctx = new
        // ClassPathXmlApplicationContext("date-properties.xml");
        ApplicationContext ctx = new FileSystemXmlApplicationContext(
                "c:\\logAnalyzer\\date-properties.xml");
        // Initializing date related data from spring configuration file
        startDate = (Date) ctx.getBean("startDate");
        delta = (Integer) ctx.getBean("delta");
    }

    public boolean isTimeDiffAcceptable(Date logDate) {

        DateTime dt1, dt2;
        Integer minutesDiff;

        dt1 = new DateTime(logDate);
        dt2 = new DateTime(startDate);

        int daysDiff = Days.daysBetween(dt1, dt2).getDays();

        if (daysDiff == 0) {
            minutesDiff = dt1.getMinuteOfDay() - dt2.getMinuteOfDay();
        } else {
            minutesDiff = 0;
        }

        // System.out.println("minutesDiff : " + minutesDiff);
        // System.out.println("delta : " + delta);

        if (minutesDiff < 0 && (-1 * minutesDiff) < delta) {
            return true;
        } else {
            return false;
        }
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Integer getDelta() {
        return delta;
    }

    public void setDelta(Integer delta) {
        this.delta = delta;
    }

}

次に、フォーマット ログ メソッドを使用すると、ログを Excel シートに入れることができます。

public class FormatLog {

    private String searchRequiredString, searchStartString, searchEndString;
    private String regex, line;
    private Integer lengthStartStr, lengthEndStr;

    private File inputFile;

    private Integer rowNum, colNum;
    private HSSFCell cell;

    private String[] ary;

    private Logger log;

    // Initializing data in constructor
    public FormatLog() {
        // initializing the logger for log4j
        log = Logger.getLogger(FormatLog.class);

        @SuppressWarnings("resource")
        ApplicationContext ctx = new ClassPathXmlApplicationContext(
                "spring.xml");

        // Initializing search String related parameters
        searchRequiredString = (String) ctx.getBean("searchRequiredString");
        searchStartString = (String) ctx.getBean("searchStartString");
        searchEndString = (String) ctx.getBean("searchEndString");
        regex = (String) ctx.getBean("regex");

        // Initializing the Log Input File
        inputFile = (File) ctx.getBean("inputFile");

        // Computing some start parameters
        lengthStartStr = searchStartString.length();
        lengthEndStr = searchEndString.length();

        // Row Number initialized to -1
        rowNum = -1;
    }

    /*
     * Input: None Output: Integer rowNum - number of rows processed.
     */
    @SuppressWarnings({ "resource", "deprecation" })
    public Integer stringManipulation() {

        // setting header text
        String[] headerText = { "Date", "Facility Name", "Work Center",
                "ID Nbr", "Preference", "Format", "Printer Name", "Qty" };
        DateFunctions df = new DateFunctions();
        try {
            FileOutputStream fileOut = new FileOutputStream(
                    "c:\\logAnalyzer\\FormattedLogsELC.xls");
            HSSFWorkbook workbook = new HSSFWorkbook();
            HSSFSheet worksheet = workbook.createSheet("ELC Log Worksheet - "
                    + df.getStartDate().toString());

            BufferedReader br = new BufferedReader(new FileReader(inputFile));
            while ((line = br.readLine()) != null) {
                // Create the StringBuilder
                StringBuilder builder = new StringBuilder(line);

                Integer indexRequired = builder.indexOf(searchRequiredString);
                if (indexRequired > 0) {

                    String date = builder.subSequence(1, 17).toString();
                    log.debug(date);
                    Date logDate = new Date(date);
                    if (df.isTimeDiffAcceptable(logDate)) {

                        log.debug(builder.toString());
                        Integer indexStart = builder.indexOf(searchStartString);
                        Integer indexEnd = builder.indexOf(searchEndString);
                        String s = builder.subSequence(
                                indexStart + lengthStartStr,
                                indexEnd - lengthEndStr + 1).toString();
                        // adding date to the string s
                        s = date + "," + s;
                        log.debug(s);
                        ary = s.split(regex);
                        if (ary.length != headerText.length) {
                            log.warn("Review row number : " + rowNum
                                    + ". Unexpected data was found");
                        }

                        // incrementing RowNumber for workbook calculation.
                        rowNum++;
                        if (rowNum == 0) {
                            log.info("Setting Header Text");
                            ary = headerText;
                        }
                        log.info("Row Number: " + rowNum
                                + " extracted for time : " + date);
                        // Create a new wb row.
                        HSSFRow row = worksheet.createRow(rowNum);
                        for (colNum = 0; colNum < ary.length; colNum++) {
                            ary[colNum] = ary[colNum].trim();
                            log.debug(ary[colNum]);
                            cell = row.createCell(colNum);
                            cell.setCellValue(ary[colNum]);
                        }// end for
                    }// end if timeDiff
                }// end if indexRequired
            } // while

            // write to workbook and close it
            workbook.write(fileOut);
            fileOut.flush();
            fileOut.close();

        } catch (FileNotFoundException e) {
            log.error(e);
        } catch (IOException e) {
            log.error(e);
        }

        if (rowNum < 0) {
            log.warn("No rows were written to the excel file.");
            log.warn("Application Logs provided may be out of the Wily alert window if no exception was thrown.");

        } else {
            log.info("Success");
        }

        // Returns the rows processed
        return rowNum;
    }
}
于 2016-03-14T06:37:36.477 に答える