簡體   English   中英

Java中解析日志文件所需的匹配模式

[英]Required matching pattern in java to parse a log file

因為我想使用匹配模式來解析日志文件以滿足以下條件,所以我在Java中很差,無法確定是否更正。 請幫忙。

    total count of report requests with size >=100kB
    total count of report requests with size <100kB
    total count of all non report requests
    total response time of report requests with size >=100kB
    total response time of report requests with size <100kB
    total response time of all non report requests
    total size of report requests with size >=100kB
    total size of report requests with size <100kB
    total size of all non report requests

日志文件的格式

 100.10.200.20 - - [19/Apr/2016:09:21:45 -0400] "GET /test/report/RenderForm.do?formId=18483&formType=CCT_APP_AP_TR_RSP HTTP/1.1" 200 418032  

用Java編寫的代碼,但請確保我的條件和模式不正確,並且我的疑問是如何獲取響應時間並更正以下Java代碼

total response time of report requests with size >=200kB
      total response time of report requests with size <200kB
    total response time of all non report requests

任何機構都可以幫助我更正此代碼,並滿足以上所有以下條件。

提前致謝。

碼:

 import java.io.BufferedReader;
  import java.io.FileReader;
  import java.io.IOException;
  import java.util.regex.Matcher;
  import java.util.regex.Pattern;

  import org.apache.log4j.Logger;



  public class Test3 {

    static Logger logger = Logger.getLogger(Test3.class);

    public static final int RETURN_CODE_SUCCESS = 0;

    public static final int RETURN_CODE_ERROR = 1;


    public static void main(String[] args) {

        BufferedReader br = null;

        try {

            String sCurrentLine;

            int nSize = 0;
            float nSizeKB = 0;

            int count = 0;
            int countNON=0, countlt100 = 0;

            String[] arLine = new String[0];
            br = new BufferedReader(new FileReader("C:\\testing.txt"));

            float gt100ReportGen=0;
            float lt100ReportGen=0;
            float gt100ResTime=0;
            float lt100ResTime=0;
            float nonReportGenSize=0;
            float nonReportResTime=0;

            while ((sCurrentLine = br.readLine()) != null) {
                Boolean bCondition1 = false;
                Boolean bCondition2 = false;
                Boolean bcondition3 = false;

                // Check the line contains the date and time

                //String pattern = ".*\\s(\\d*)";

                //Pattern r = Pattern.compile(pattern);

              //  Matcher m = r.matcher(sCurrentLine);

                Pattern p = Pattern.compile("\\d\\d\\d\\d-\\d\\d-\\d\\d@\\d\\d:\\d\\d:\\d\\d");

                Matcher m = p.matcher(sCurrentLine);

                arLine = sCurrentLine.split(" ");
                nSize = arLine.length-1;
                nSizeKB = nSize/1024;

                 if (sCurrentLine.contains("report")){

                     count++;

                    if (nSizeKB < 100) {

                        //total size of ReportGen requests with size <100kB
                        lt100ReportGen=lt100ReportGen+nSizeKB;

                        bCondition1 = true;

                        //total count of ReportGen requests with size <100kB
                        countlt100++;
                    }
                    if (nSizeKB >= 100) {

                        // total size of ReportGen requests with size >=100kB
                        gt100ReportGen=gt100ReportGen+nSizeKB;
                        bCondition2 = true;
                    }
                 }  else {
                     //total count of all non ReportGen requests
                     countNON++;
                     //total size of all non ReportGen requests
                     nonReportGenSize=nonReportGenSize+nSizeKB;
                     bcondition3=true;
                 }

                 if (m.find()) {

                     if (bCondition1) {
                         //total response time of ReportGen requests with size <100kB
                         lt100ResTime=lt100ResTime+nSizeKB;
                     }
                     if (bCondition2) {
                         //total response time of ReportGen requests with size >=100kB
                         gt100ResTime=gt100ResTime+nSizeKB;
                     }
                     if (bcondition3){
                         //total response time of all non ReportGen requests
                         nonReportResTime=nonReportResTime+nSizeKB;
                     }
                 }
            }

            logger.info("total count of ReportGen requests with size <100kB: "+countlt100);
            logger.info("# of Non-ReportGEN Requests: "+countNON);
            logger.info("total response time of ReportGen requests with size >=100kB: "+gt100ReportGen);
            logger.info("total response time of ReportGen requests with size <100kB: "+lt100ReportGen);
            logger.info("total response time of all non ReportGen requests: "+nonReportResTime);
            logger.info("total size of ReportGen requests with size <100kB: "+lt100ResTime);
            logger.info("total size of ReportGen requests with size >=100kB: "+gt100ResTime);
            logger.info("total size of all non ReportGen requests: "+nonReportGenSize);


            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.append("total count of ReportGen requests with size <100kB: "+ countlt100);
            stringBuilder.append(", # of Non-ReportGEN Requests: "+countNON);
            stringBuilder.append(", total response time of ReportGen requests with size >=100kB: "+gt100ReportGen);
            stringBuilder.append(", total response time of ReportGen requests with size <100kB: "+lt100ReportGen);
            stringBuilder.append(",total response time of all non ReportGen requests: "+nonReportResTime);
            stringBuilder.append(",total size of ReportGen requests with size <100kB: "+lt100ResTime);
            stringBuilder.append(",total size of ReportGen requests with size >=100kB: "+gt100ResTime);
            stringBuilder.append(",total size of all non ReportGen requests: "+nonReportGenSize);


            logger.info(stringBuilder.toString());

            System.exit(RETURN_CODE_SUCCESS);


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

            System.exit(RETURN_CODE_ERROR);

        } finally {
            try {
                if (br != null)br.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }



    }
  }

您可以在不使用正則表達式的情況下獲得計數和尺寸結果,如下所示:

            List<String> log = Arrays.asList(
            "100.10.200.20 - - [19/Apr/2016:09:21:45 -0400] \"GET /test/report/RenderForm.do?formId=18483&formType=CCT_APP_AP_TR_RSP HTTP/1.1\" 200 418032",
            "100.10.200.20 - - [19/Apr/2016:09:21:45 -0400] \"GET /test/nonreport/RenderForm.do?formId=18483&formType=CCT_APP_AP_TR_RSP HTTP/1.1\" 200 418032",
            "100.10.200.20 - - [19/Apr/2016:09:21:45 -0400] \"GET /test/report/RenderForm.do?formId=18483&formType=CCT_APP_AP_TR_RSP HTTP/1.1\" 200 18032",
            "100.10.200.20 - - [19/Apr/2016:09:21:45 -0400] \"GET /test/nonreport/RenderForm.do?formId=18483&formType=CCT_APP_AP_TR_RSP HTTP/1.1\" 200 18032"
            );

    int countRequestsGE100 = 0;
    int countRequestsL100 = 0;
    int countNonReportRequests = 0;
    int sizeRequestsGE100 = 0;
    int sizeRequestsL100 = 0;
    int sizeNonReportRequests = 0;
    int urlOffset = 6;
    int sizeOffset = 9;
    for(String s:log)
    {
        String[] array = s.split(" ");
        int size = Integer.valueOf(array[sizeOffset]).intValue();
        if (array[urlOffset].contains("/report/"))
        {
            if (Integer.valueOf(array[sizeOffset]).intValue() >= 100000)
            {
                countRequestsGE100++;
                sizeRequestsGE100 += size;
            }
            else
            {
                countRequestsL100++;
                sizeRequestsL100 += size;
            }
        }
        else
        {
            countNonReportRequests++;
            sizeNonReportRequests += size;
        }
    }

    System.out.print(
            "total count of report requests with size >=100kB: " + countRequestsGE100 + "\n" +
            "total count of report requests with size <100kB: " + countRequestsL100 + "\n" +
            "total count of all non report requests: " + countNonReportRequests + "\n" +
            "total size of report requests with size >=100kB: " + sizeRequestsGE100 + "\n" +
            "total size of report requests with size <100kB: " + sizeRequestsL100 + "\n" +
            "total size of all non report requests: " + sizeNonReportRequests + "\n"
            );

但是我看不到您的生產線中可以計算總響應時間的地方

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM