question 473 Observability & Operations · easy
Read one structured log line
Log tools read logs one line at a time, and must never crash on a line they can't use. Turn one line of text into a log entry, or None if it isn't a usable entry.
Function
parse_log_line(line)Usablethe line (ignoring spaces at either end) is a JSON object whose
level is one of DEBUG, INFO, WARNING, ERROR, CRITICAL and whose message is textReturnsthe parsed dictionary for a usable line;
None for anything else (not JSON, not an object, missing or unknown level, missing message)Examples
callparse_log_line('{"level":"ERROR","message":"query failed","trace":"t-7f3a"}')returns{'level': 'ERROR', 'message': 'query failed', 'trace': 't-7f3a'}
callparse_log_line('WARNING: connection pool is almost full')returnsNone
callparse_log_line('{"level":"INFO"}')returnsNone