Sujan SM
Sujan SM

Reputation: 21

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

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.

Upvotes: 1

Views: 261

Answers (3)

segev_gr
segev_gr

Reputation: 111

You can try:

import json

log_file_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"
log_values_in_arr = log_file_output .split(" : ")
log_keys_in_dict = {"Error": None, "Assertion": None, "Message": None,"Coverage": None,"Severity": None, "Time": None}
log_values_arr_counter = 0
for key in log_keys_in_dict:
    log_keys_in_dict[key] = log_values_in_arr[log_values_arr_counter]
    log_values_arr_counter = log_values_arr_counter+1

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


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

Upvotes: 0

0x0fba
0x0fba

Reputation: 1620

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}

Upvotes: 1

user16004728
user16004728

Reputation:

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)

Upvotes: 0

Related Questions