0

解析原始日志文件转储并提取相关信息。

以下是两个事件的日志文件提取。每个事件由* *分隔,如下所示:

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 程序将它输入到 MS Excel 中。

以下是我需要从上面的原始数据转储中获取的相关信息:

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]

此原始数据始终以“CS 标签:”开头,后跟“[”,然后有 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 回答