runtime
236 / 500
question 236 Debugging & Logging · medium

Problems per logger

An on-call engineer wants to know which part of a program is producing problems. The log was written with the format LEVEL name: message, and tracebacks are mixed in. Write level_counts(lines, min_level): count, per logger name, the records at min_level or more serious.

Functionlevel_counts(lines, min_level)
LevelsDEBUG < INFO < WARNING < ERROR < CRITICAL, by severity (not alphabetically)
Recorda line whose first word is one of those five level names, followed by the logger name, : and the message; the message itself may contain :
Other linestraceback lines and anything else are not records and are ignored
Returnsa dictionary from logger name to count; loggers with no qualifying records don't appear
RaisesValueError if min_level is not one of the five names
Examples
calllevel_counts(['INFO prices: loaded 3 lines', "WARNING prices: line 2: bad price 'x'", 'ERROR sync: could not sync lyon', 'Traceback (most recent call last):', ' File "main.py", line 9, in sync_all', 'OSError: connection reset', 'WARNING prices: line 7: expected 2 fields'], 'WARNING')returns{'prices': 2, 'sync': 1}
calllevel_counts(['INFO prices: loaded 3 lines', "WARNING prices: line 2: bad price 'x'", 'ERROR sync: could not sync lyon', 'Traceback (most recent call last):', ' File "main.py", line 9, in sync_all', 'OSError: connection reset', 'WARNING prices: line 7: expected 2 fields'], 'ERROR')returns{'sync': 1}
calllevel_counts(['INFO prices: loaded 3 lines', "WARNING prices: line 2: bad price 'x'", 'ERROR sync: could not sync lyon', 'Traceback (most recent call last):', ' File "main.py", line 9, in sync_all', 'OSError: connection reset', 'WARNING prices: line 7: expected 2 fields'], 'INFO')returns{'prices': 3, 'sync': 1}

Loading your progress…

Problems per logger — Python practice · Runtime