Home > Mobile >  How can i convert this log data into JSON using a python script
How can i convert this log data into JSON using a python script

Time:11-10

I have log file from a Vivado simulator, which i want to convert into simple JSON to visualize it ultimately. Please suggest me a python code to format the logs into JSON.

I have tried to search for converting the logs into JSON, but most of them convert .csv (comma separated values) into JSON, while my log file contains colon separated values.

This is line from my log file:

OVL_ERROR : ASSERT_NO_OVERFLOW : Counter did not reset after reaching Threshold : Test expression changed value from allowed maximum value max to a value in the range max 1 to min : severity 1 : time 430000 : counter_tb.no_overflow.ovl_error_t

I want the JSON to look like this:

{
"Error":"OVL_Error",
"Assertion":"ASSERT_NO_OVERFLOW",
"Message":"Counter_did_not_reset_after_reaching_Threshold",
"Coverage":"Test expression changed value from allowed maximum value max to a value in the range max 1 to min",
"Severity":"1",
"Time":"430000"
}

Is it possible to do so.

Thanks.

CodePudding user response:

A solution that uses the zip() function and a dict-comprehension.

Severity and Time are converted to integer.

line = "OVL_ERROR : ASSERT_NO_OVERFLOW : Counter did not reset after reaching Threshold : Test expression changed value from allowed maximum value max to a value in the range max 1 to min : severity 1 : time 430000 : counter_tb.no_overflow.ovl_error_t"

logkeys = ("Error", "Assertion", "Message", "Coverage", "Severity", "Time")
logvalues = [x.strip() for x in line.split(":")[:-1]]
logline = {k:v if i <4 else int(v.rsplit(" ", 1)[-1]) for i, (k, v) in enumerate(zip(logkeys, logvalues))}

print(logline)
{'Error': 'OVL_ERROR',
 'Assertion': 'ASSERT_NO_OVERFLOW',
 'Message': 'Counter did not reset after reaching Threshold',
 'Coverage': 'Test expression changed value from allowed maximum value max to a value in the range max 1 to min',
 'Severity': 1,
 'Time': 430000}

CodePudding user response:

You can do something like this:

fileDesc = open('YourFileName', 'r')
fileData = fileDesc.read()
fileDesc.close()

log = []

for line in fileData.splitlines():
    words = [word.strip() for word in line.split(':')]
    log.append({
        'Error': words[0],
        'Assertion': words[1],
        'Message': words[2],
        'Coverage': words[3],
        'Severity': words[4],
        'Time': words[5]
    })

print(log)

CodePudding user response:

You can try:

import json

log_output = "OVL_ERROR : ASSERT_NO_OVERFLOW : Counter did not reset after reaching Threshold : Test expression changed value from allowed maximum value max to a value in the range max 1 to min : severity 1 : time 430000 : counter_tb.no_overflow.ovl_error_t"
arr_log = log_output.split(" : ")
dict_write = {"Error":None ,"Assertion":None ,"Message":None ,"Coverage":None ,"Severity":None ,"Time":None}
counter = 0
for index in dict_write:
    dict_write[index] = arr_log[counter]
    counter = counter 1

json_object = json.dumps(dict_write, indent=4)


with open("log.json", "w") as outfile:
    outfile.write(json_object)
  • Related