上下文: 這是一個日誌分析的事情。我正在創建一個regex
程序來查找從客戶端發送到服務器的某些請求的發生。我有包含這些請求的客戶端日誌文件以及其他日誌。如何在模式重疊時發現發生
問題: 當一個請求消息發送給服務器,客戶端應該有像2個日誌語句:
sending..
message_type
當上述陳述或圖案發現,我們可以說一個請求已發送。它是組合模式。好吧
我們期待日誌文件的內容會像
sending..
message_type
...//other text
sending..
message_type
...//other text
sending..
message_type
從上面的日誌,我們可以說客戶端已派出3個消息。但在實際的日誌文件不知何故,模式如下重疊(不是所有的消息,但對於一些):
sending..(1)
...//other text
sending..(2)
message_type(2)
...//other text
message_type(1)
sending..(3)
message_type(3)
還有3請求(我編號信息來了解)。但是這種模式是重疊的,即在完全記錄第一條消息之前,記錄第二條消息。 以上說明僅供參考。下面是原來的日誌的部分:
原始日誌
Send message to server:
Created post notification log dir
Created post notification log dir
Created post notification log dir
Send message to server:
Created post notification log dir
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><message schema_version="3644767c-2632-411a-9416-44f8a7dee08e"><request xaction_guid="new xaction guid" type="createsession"/></message>
INFO [a] - Server Response: <?xml version="1.0" encoding="UTF-8" standalone="yes"?><message schema_version="3644767c-2632-411a-9416-44f8a7dee08e"><response xaction_guid="new xaction guid" type="ok"></params></response></message>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><message schema_version="3644767c-2632-411a-9416-44f8a7dee08e"><request xaction_guid="new xaction guid" type="createsession"/></message>
INFO [a] - Server Response: <?xml version="1.0" encoding="UTF-8" standalone="yes"?><message schema_version="3644767c-2632-411a-9416-44f8a7dee08e"><response xaction_guid="new xaction guid" type="ok"></response></message>
這裏每解釋一個請求將與2份鑑定:
Send message to server:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><message schema_version="3644767c-2632-411a-9416-44f8a7dee08e"><request xaction_guid="new xaction guid" type="createsession"/></message>
我試過
public class LogMatcher {
static final String create_session= "Send message to server(.){10,1000}(<\\?xml(.){10,500}type=\"createsession\"(.){1,100}</message>)";
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("D:/dummy.txt"))));//I put the above log in this file
StringBuilder b = new StringBuilder();
String line = "";
while((line = reader.readLine()) != null){
b.append(line);
}
findMatch(b,"Send message to server","Send message to server");
findMatch(b,create_session,"create_session");
}
private static int findMatch(StringBuilder b,String pattern, String type) {
int count =0;
Pattern regex = Pattern.compile(pattern,Pattern.MULTILINE);
Matcher regexMatcher = regex.matcher(b.toString());
while (regexMatcher.find()) {
count++;
}
System.out.printf("%25s%2d\n",type+": ",count);
return count;
}
}
電流輸出
目的是要找出createsession
消息的號碼發送
Send message to server: 2
create_session: 1
預計輸出
從日誌很顯然,2個消息sent.So放出來會是:
Send message to server: 2
create_session: 2
你可以看到我在代碼中嘗試過的模式。任何人都可以提出一個模式來獲得所需的結果?
注:可以簡單地說爲什麼不單獨使用計數Send message to server
。因爲在日誌中有很多類型的消息,比如login, closesession
等等。所有這些消息的第一部分都是Send message to server
。此外,他們已經單獨記錄的消息類型用於其他目的,所以我們不能對任何部分(這意味着我們能對繼電器的組合)的某些請求
那麼,爲什麼你不指望'型= \ 「了createSession \」'一個人嗎? (1)'+'發送(2)'+'消息(1)'+'消息(2)'? – Mariano
爲什麼因爲這些'type = \「createsession \」'xmls會與'Send message to server'一起記錄,還有一些其他的方式,比如存儲在'db'中。所以我們不能簡單地依靠這個。 –
那麼如何知道'Send message to server'和你想匹配的xml之間是否存在'type = \「createsession \」'的'*其他方式*'?你可以展示一個你想忽略的xml的例子嗎? – Mariano