Reputation: 284
I need to parse a log file in java. The log files contains information about a call transaction. The lines that I'm interested in and also the information within are marked in bold within the log line entry example. For the following one I need to extract, the Status and phone number:
Aug 15 20:35:22 GMT 2012 tropo109.orl.voxeo.net TROPO 138595 0 e467547d3333724bdd52635bbb713e77 1 d607eb64fb3bfbfd273a55f4b121b903 SimpleOutgoingCall[+17877058826/null->+17877260664/null] : [TRANSFER,*+17877260664*]
and from this line I need to obtain the phone number and the total calling time.
Aug 15 20:35:22 GMT 2012 tropo109.orl.voxeo.net TROPO 138595 0 e467547d3333724bdd52635bbb713e77 1 d607eb64fb3bfbfd273a55f4b121b903 Logging CDR {"call":{"SipSessionID":"ss_jzto5yd4jruv","SessionID":"e467547d3333724bdd52635bbb713e77","CallID":"d607eb64fb3bfbfd273a55f4b121b903","ParentSessionID":"none","ParentCallID":"none","DateCreated":"Wed, 15 Aug 2012 20:34:14 +0000","DateUpdated":"Wed, 15 Aug 2012 20:34:14 +0000","AccountID":"138595","Called":"+17877260664","Caller":"+17877058826","PhoneNumberSid":"unknown","Disposition":"Script ended","Status":"Success","StartTime":"Wed, 15 Aug 2012 20:34:14 +0000","EndTime":"Wed, 15 Aug 2012 20:35:22 +0000","Duration":"67950","Flags":"out","RecordingDuration":"0","Network":"SIP","Channel":"VOICE","ApplicationId":"392671","ApplicationType":"groovy","ServiceId":"1291899","StartUrl":"http://hosting.tropo.com/138595/www/outboud-web2ivr.groovy","BrowserIP":"10.6.69.109","PPID":"461"}}
So in general I'll need to parse a file and within different log lines entries get the call status and duration. Any pointers?
UPDATE: I got the code to get the first part, any pointer on how to get the second and have just one compiled expression that either will find (phone,status) or (phone,callduration) within a log entry line?:
private static void matchParts( String aText ){
Pattern pattern = Pattern.compile("(?:\\[(\\w(\\w)*),(\\+\\d{11})\\])");
Matcher matcher = pattern.matcher(aText );
String phone;
String status;
while (matcher.find()) {
System.out.println("phone:" +matcher.group(3) + ", status: "+matcher.group(1) );
}
}
Upvotes: 2
Views: 2593
Reputation: 3895
For the first one:
private static void matchParts(String line){
Pattern pattern = Pattern.compile(": \\[(\\w+),\\*(\\+\\d{11})\\*]");
Matcher matcher = pattern.matcher(line);
String phone;
String status;
while (matcher.find()) {
System.out.println("phone:" +matcher.group(2) + ", status: "+matcher.group(1) );
}
}
For the second:
private static void matchParts(String line){
Pattern pattern = Pattern.compile("Called\":\"(\\+\\d{11}).*\"Duration\":\"(\\d+)");
Matcher matcher = pattern.matcher(line);
String phone;
String status;
while (matcher.find()) {
System.out.println("phone:" +matcher.group(1) + ", duration: "+matcher.group(2) );
}
}
I use this very handy tool: http://www.regexplanet.com/advanced/java/index.html when it comes to finding regexes.
EDIT: If you are looking for 1 regex instead, it could be something like this:
Pattern.compile("\\[(\\w+),\\*(\\+\\d{11})\\*].*?Called\":\"(\\+\\d{11}).*?\"Duration\":\"(\\d+)", Pattern.MULTILINE);
Upvotes: 1
Reputation: 24124
To me, the portion of the log after ":" in the first one looks like a JSON Array and the portion of the log after CDR in the second section looks like a JSON Map. You could use a simple JSON library in any language of your choice to convert those log sections and access the required information in a structured way: http://json.org
Upvotes: 1
Reputation: 283
Is this all one line or multiple lines?
If the first one is multiple lines, then:
String line = yourline
if(line.indexOf("SimpleOutgoingCall" != -1)
{
String data = line.split(":")[1];
String status = data.substring(1, data.indexOf(","));
}
The second one looks like the first two are different lines, but the {{ ... }} block is one line" If so:
String line = yourline
String data = line.split(",");
String called, duration;
for(int x = 0; x < data.length; x++)
{
if(data[x].indexOf("Called") != -1)
called = data[x].split(":")[1];
if(data[x].indexOf("Duration") != -1)
duration = data[x].split(":")[1];
}
Upvotes: 0