2016-04-28 87 views
0

因爲我想使用匹配模式解析日誌文件以滿足以下條件,所以我在java中很窮,不確定是否正確。請幫助。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\\[email protected]\\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(); 
      } 
     } 



    } 
    } 

回答

0

可以得到數量和大小的結果,而正則表達式如下:

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

但我沒有看到你行的地方,計算總響應時間