Home > Back-end >  Parse a log file into json object
Parse a log file into json object

Time:07-01

I have log file named request.log this is the content of the log file

[2022-06-30T09:56:40.146Z] ### POST https://test.csdf/auth/send_otp  
    
 {
  "method": "POST",
  "headers": {
    "User-Agent": "testing",
    "Content-Type": "application/json"
  }
}
[2022-06-30T09:56:40.668Z] ### POST https://test.csdf/auth/login  
    
 {
  "method": "POST",
  "headers": {
    "User-Agent": "testing",
    "Content-Type": "application/json"
  }
}

how can I parse the log file and transform it into array json object like this

[
  {
     "method": "POST",
     "url": "https://test.co/otp"
  },
  {
     "method": "POST",
     "url": "https://test.co/login"
  }
]

CodePudding user response:

You have to run some kind of script to convert them to JSON. In javascript/nodejs you could do something like this:

const logFileContents = `[2022-06-30T09:56:40.146Z] ### POST https://test.csdf/auth/send_otp  
    
{
 "method": "POST",
 "headers": {
   "User-Agent": "testing",
   "Content-Type": "application/json"
 }
}
[2022-06-30T09:56:40.668Z] ### POST https://test.csdf/auth/login  
   
{
 "method": "POST",
 "headers": {
   "User-Agent": "testing",
   "Content-Type": "application/json"
 }
}`;

function convertToJSON(logs) {
  return logs
    .match(/\[. \]\s\# \s\w \s.*\n/g) // fetch lines to parse
    .map((line) => line.split('###')[1].trim().split(' ')) //isolate method/url
    .map((log) => ({
      method: log[0],
      url: log[1],
    }));// convert array to object
}

console.log(JSON.stringify(convertToJSON(logFileContents), null, 4));

if it doesn't work for your log files, just fix the regex to match your case

  • Related