正则表达式解析日志

时间:2018-02-04 23:14:07

标签: java regex regex-lookarounds regex-greedy

你好黑客想知道是否有人可以提供帮助我需要匹配所有类型的日志以及如何匹配{}中的完整对象的任何建议吗?

样品

[1517725731.300][DEBUG]: DEVTOOLS EVENT Runtime.consoleAPICalled {
   "args": [ {
      "className": "Array",
      "description": "Array(2)",
      "objectId": "{\"injectedScriptId\":13,\"id\":11}",
      "preview": {
         "description": "Array(2)",
         "overflow": false,
         "properties": [ {
            "name": "0",
            "type": "string",
            "value": "tracking fired"
         }, {
            "name": "1",
            "subtype": "array",
            "type": "object",
            "value": "Arguments(4)"
         } ],
         "subtype": "array",
         "type": "object"
      },
      "subtype": "array",
      "type": "object"
   } ],
   "executionContextId": 13,
   "stackTrace": {
      "callFrames": [ {
         "columnNumber": 39,
         "functionName": "window.debug.that.(anonymous function)",
         "lineNumber": 184,
         "scriptId": "234",
         "url": "https://secure.somerandomsite.com/js/common/ba-debug.js"
      }, {
         "columnNumber": 475,
         "functionName": "loggerTrackingHandler",
         "lineNumber": 0,
         "scriptId": "60",
         "url": "https://secure.somerandomsite.com/jsmin/gzip_1642294/bundles/mainTracking.min.js"
      }, {
         "columnNumber": 436,
         "functionName": "CsApplicationTrackingControllerBase.commontTrackEvent",
         "lineNumber": 4,
         "scriptId": "60",
         "url": "https://secure.somerandomsite.com/jsmin/gzip_1642294/bundles/mainTracking.min.js"
      }, {
         "columnNumber": 5,
         "functionName": "CsApplicationTrackingControllerBase.parentTrackEvent",
         "lineNumber": 8,
         "scriptId": "60",
         "url": "https://secure.somerandomsite.com/jsmin/gzip_1642294/bundles/mainTracking.min.js"
      }, {
         "columnNumber": 525,
         "functionName": "CsApplicationTrackingController.trackEvent",
         "lineNumber": 16,
         "scriptId": "60",
         "url": "https://secure.somerandomsite.com/jsmin/gzip_1642294/bundles/mainTracking.min.js"
      }, {
         "columnNumber": 2124,
         "functionName": "CsApplicationTrackingController.trackChangeTab",
         "lineNumber": 19,
         "scriptId": "60",
         "url": "https://secure.somerandomsite.com/jsmin/gzip_1642294/bundles/mainTracking.min.js"
      }, {
         "columnNumber": 22,
         "functionName": "MyCreditAnalysisViewModel.self.selectTab",
         "lineNumber": 6,
         "scriptId": "263",
         "url": "https://secure.somerandomsite.com/jsmin/gzip_N1393604112/bundles/newOverview.min.js"
      }, {
         "columnNumber": 48,
         "functionName": "",
         "lineNumber": 53,
         "scriptId": "281",
         "url": "https://secure.somerandomsite.com/js/common/knockout/knockout-2.1.0.js"
      }, {
         "columnNumber": 4815,
         "functionName": "dispatch",
         "lineNumber": 2,
         "scriptId": "228",
         "url": "https://secure.somerandomsite.com/3rd_party/jquery-1.7.2.min.js"
      }, {
         "columnNumber": 708,
         "functionName": "i",
         "lineNumber": 2,
         "scriptId": "228",
         "url": "https://secure.somerandomsite.com/3rd_party/jquery-1.7.2.min.js"
      } ]
   },
   "timestamp": 1517725731298.069,
   "type": "log"
}
[1517725731.305][DEBUG]: DEVTOOLS RESPONSE Input.dispatchMouseEvent (id=260) {

}
[1517725731.305][INFO]: Waiting for pending navigations...
[1517725731.305][DEBUG]: DEVTOOLS COMMAND Runtime.evaluate (id=261) {
   "expression": "1"
}

1 个答案:

答案 0 :(得分:0)

任何以[1517725731.305][INFO]:开头的行都是新的日志条目。寻找那些。

public static void processLogFile(Path logFile) throws IOException {
    Pattern logstart = Pattern.compile("^\\[\\d+\\.\\d+\\]\\[\\w+\\]: ");
    try (BufferedReader log = Files.newBufferedReader(logFile)) {
        StringBuilder logEntry = new StringBuilder();
        for (String line; (line = log.readLine()) != null; ) {
            if (logstart.matcher(line).find()) {
                if (logEntry.length() != 0)
                    processLogEntry(logEntry.toString());
                logEntry.setLength(0); // clear buffer
            }
            logEntry.append(line).append(System.lineSeparator());
        }
        if (logEntry.length() != 0)
            processLogEntry(logEntry.toString());
    }
}
private static void processLogEntry(String logEntry) {
    // code here
}