easy +8 pts

Logging Filter

Filter log entries by level and substring using pure Python.

You are given a list of log entries, each a string like `"2024-01-15 INFO User logged in"`. Write a function `filter_logs(logs, level, keyword)` that returns a new list with only the entries whose severity level (the second whitespace-separated token) equals `level` (case-insensitive) AND whose text contains `keyword` as a substring (case-insensitive). The returned list should preserve the original order. If no entries match, return an empty list.

Constraints

`logs` length 0 to 10,000. Each log string is non-empty and contains at least two whitespace-separated tokens. `level` and `keyword` are non-empty strings. The keyword may appear anywhere in the log text, including within the level or timestamp.

Example

>>> logs = [
...     "2024-01-15 INFO User logged in",
...     "2024-01-15 ERROR Disk full",
...     "2024-01-16 INFO Backup started",
...     "2024-01-16 WARNING Low memory"
... ]
>>> filter_logs(logs, "info", "user")
['2024-01-15 INFO User logged in']
>>> filter_logs(logs, "ERROR", "disk")
['2024-01-15 ERROR Disk full']
>>> filter_logs(logs, "WARN", "memory")
[]
8 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Split each log string into tokens with `.split()` to get the level at index 1.
Compare levels using `.lower()` so the match is case-insensitive.
Check if `keyword.lower()` is in `log.lower()` to match the substring case-insensitively.
Use a list comprehension to keep matching entries in order.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.