avatar Bite 315. More logging practice

In this Bite you will hone your logging skills by completing sum_even_numbers following the instructions in its docstring.

Not only will you code the function you will also deal with bad data:

- If you got an exception you will log and reraise it.

- If there is no exception, you will just log what the function did (example in docstring).

Here is how it should work:

# Scenario I. no exception
>>> from logger import sum_even_numbers
>>> sum_even_numbers([1, 2, 3, 4])
6
# this got logged:
>>> with open('/tmp/app.log') as f:
...     print(*f.readlines(), sep="\n")
...
2020-12-20 09:56:02,049 INFO sum_even_numbers Input: [1, 2, 3, 4] -> output: 6

# Scenario II. exception
>>> from logger import sum_even_numbers

# reraise the exception
>>> sum_even_numbers([1, 2, 'a', 4])
...
TypeError: not all arguments converted during string formatting

# this got logged:
>>> with open('/tmp/app.log') as f:
...     print(*f.readlines(), sep="\n")
...
2020-12-20 09:57:38,706 ERROR sum_even_numbers Bad inputs: [1, 2, 'a', 4]

Traceback (most recent call last):
...
TypeError: not all arguments converted during string formatting

--

If needed check out Python's Logging HOWTO. Happy coding!

Login and get coding
go back Intermediate level
Bitecoin 3X

33 out of 34 users completed this Bite.
Will you be the 34th person to crack this Bite?
Resolution time: ~43 min. (avg. submissions of 5-240 min.)

Focus on this Bite hiding sidebars, turn on Focus Mode.

Ask for Help