java - parse json objects from a log file -


i want parse json objects log file. using json parser complete files has in json format not case me. there way can parse file line line , json objects. below log file format:

2015-10-19 11:24:35:701 info  brokertcpclient:28 - set destination  2015-10-19 11:24:35:929 debug brokertcpclient:32 - received data: {type=data,  payload={      "core" : [ {       "id" : {         "datatype" : "http://www.w3.org/2001/hk#long",         "type" : "gh",         "value" : "gh"       },       "entity" : {         "type" : "uri",         "value" : "http://fg.fg.com/ext/g/fg"       },       "sno" : {         "type" : "literal",         "value" : "fg"       }] 2015-10-19 11:24:35:701 info  brokertcpclient:28 - set destination  2015-10-19 11:24:35:929 debug brokertcpclient:32     "core" : [ {       "id" : {         "datatype" : "http://www.w3.org/2001/hk#long",         "type" : "gh",         "value" : "gh"       },       "entity" : {         "type" : "uri",         "value" : "http://fg.fg.com/ext/g/fg"       },       "sno" : {         "type" : "literal",         "value" : "fg"       }] 

can 1 please how should json objects. when trying parse single line of json objects throwing exception.

here solution works sample log posted.

import java.io.*; import com.fasterxml.jackson.databind.*; public class jsontest {     public static void main(string[] args) {         string logfilename = "c://temp/sample.log";         string line, json = "";          try (bufferedreader br = new bufferedreader(new filereader(logfilename))) {             while ((line = br.readline()) != null) {                 if (islogline(line)) {                     if (!json.isempty()) {                         parsejson(json);                         json = "";                     }                 } else {                     json += line;                 }             }         } catch (exception e) {             e.printstacktrace();         }     }      public static boolean islogline(string line) {         return line.matches("^\\d{4}\\-\\d{2}\\-\\d{2}\\s\\d{2}:\\d{2}:\\d{2}:\\d{3}.+$");     }      public static void parsejson(string json) throws exception {         if (!json.startswith("{") && !json.endswith("}")) json = "{" + json + "}";         objectmapper om = new objectmapper();         system.out.println(om.readvalue(fixjson(json), object.class));     }      public static string fixjson(string json) {         return "{" + json.replace("}]", "}}]") + "}";     } } 

notes:

  1. the regex identifies log line checking timestamp @ begining of line. not work in case log message spans multiple lines (for example if message contains new line)
  2. there method attempts "fix" incomplete json log file, may need additional logic, if there more cases of incomplete json along way
  3. i used jackson json parser , let objectmapper figure out data structure parse into

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -