Debugging and testing · 12 / 14 Exercise Medium

Logging records

Objective

Create a Python program that uses the logging module to record messages with different levels and show only the important ones.

Instructions

  • Import logging and sys. The first line of the input is the minimum level (DEBUG, INFO, WARNING, ERROR or CRITICAL). Configure the logging with logging.basicConfig(level=getattr(logging, minimum), format="%(levelname)s: %(message)s", stream=sys.stdout, force=True).
  • Read an integer n and then n lines with the format LEVEL message (split with split(" ", 1)). Record each message with logging.log(getattr(logging, level), message).
  • logging will show only the messages whose level is equal to or higher than the minimum, with the format LEVEL: message.
  • At the end show with print the line Received: N.
Input
WARNING
4
INFO server started
ERROR disk full
DEBUG x = 3
WARNING low memory
Output
ERROR: disk full
WARNING: low memory
Received: 4

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise