Search RU
Tasks / Logs / Parsing JSON logs and filtering by fields
Statement medium

Parsing JSON logs and filtering by fields

Implement parsing for structured logs.

`parse_log_line(line)` takes one JSON log line and returns a dict:

- required fields: `ts`, `level`, `message`;
- `ts` is an ISO-8601 string; the result is an aware `datetime` in UTC;
- both `Z` and a `+03:00` offset are normalized to UTC;
- ISO-8601 without a zone is treated as UTC;
- `level` is uppercased; `DEBUG`, `INFO`, `WARNING`, `ERROR` are allowed;
- `message` must be a string;
- other fields are preserved unchanged;
- an empty line, broken JSON, non-object JSON, a missing required field, a wrong
type or an unknown level — `LogFormatError`;
- `LogFormatError` subclasses `ValueError`.

`filter_records(records, level=None, service=None, min_duration_ms=None,
message_contains=None)`:

- all supplied filters are combined with **AND**;
- `level` is compared case-insensitively;
- `service` is an exact match;
- `min_duration_ms` keeps records with `duration_ms >= value`; records without a
numeric `duration_ms` are dropped;
- `message_contains` is a case-insensitive substring;
- with no filters all records are returned in their original order;
- the input list and its records are not mutated.

logsjsonparsingfiltering
solution.py Python 3.14
AI mentor

The AI mentor is available after you log in: it sees your attempt history and does not spend resources on anonymous requests.

Log in and ask
Hints 1 of 3 unlocked · attempts: 0
Hint 1

Which fields are required for a record to count as a log at all? And what should
happen with a `ts` that has no zone?

Hint 2 — unlocks after 1 attempt
Hint 3 — unlocks after 3 attempts