1

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.

1
  • Basically - split the log line at :, then zip respective values with the keys into a dict and write as JSON. Show your code and ask specific question Commented Nov 10, 2022 at 7:01

3 Answers 3

1

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}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks man, got what i needed, Also can you share what i can refer to write zip() function for other custom logs in similar formats.
zip() is a builtin function that "iterates over several iterables in parallel, producing tuples with an item from each one." Official documentation and examples here.
0

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)

Comments

0

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)

2 Comments

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
The answer has been fixed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.