I’m making a program which iterates through a lot of jobs and prints their traces (error, warn, info logs). It is quite time consuming so I’m concerned on performance. I have the next code:
def print_job_info(browser_logs, name):
print(f"{name} LOGSn")
print(".............ERROR..........n")
unique_errors = set()
for line in browser_logs['ERROR']:
# Eliminar la información irrelevante al principio de la línea
line_without_beginning_info = re.sub(r'^.*CEST 2024 ', '', line)
unique_errors.add(line_without_beginning_info)
for line in unique_errors:
print('ERROR: ' + line + 'n')
if CONFIG['level'] > 0:
print("n.......WARN.........n")
unique_warnings = set()
for line in browser_logs['WARN']:
# Eliminar la información irrelevante al principio de la línea
line_without_beginning_info = re.sub(r'^.*CEST 2024 ', '', line)
unique_warnings.add(line_without_beginning_info)
for line in unique_warnings:
print('WARN: ' + line + 'n')
if CONFIG['level'] > 1:
print("n......DEBUG+INFO.......n")
unique_debug = set()
for line in browser_logs['DEBUG']:
# Eliminar la información irrelevante al principio de la línea
line_without_beginning_info = re.sub(r'^.*CEST 2024 ', '', line)
unique_debug.add(line_without_beginning_info)
for line in unique_debug:
print('DEBUG: ' + line + 'n')
print("n")
This prints the information about each job. The program has a level of “debug”, depending on its level, it will print the ERROR (level 0), the ERROR and WARN (level 1)…etc.
The thing is I have thought about adding some if
statements so that the LOGS print is only printed when there are any traces to be shown in the job. Same with the …ERROR… or the …WARN… only shown when there are traces shown of their type so that performance is improved by not printing. Or do you think if
statements will have a bad impact on performance? Better to put some if
statements than printing LOGS, …ERROR… etc in all the jobs?
As I said, there are many jobs and it can be executing many seconds or minutes depending so it is not trivial.
2