Learn

The logging module has several methods that we can use to log messages and errors with an assigned severity level. Those methods are:

  • debug(msg) which logs a message with level DEBUG
  • info(msg) which logs a message with level INFO
  • warning(msg) which logs a message with level WARNING
  • error(msg) which logs a message with level ERROR
  • critical(msg) which logs a message with level CRITICAL

Using our logger object, we can call these methods directly while inputting our desired log message as the parameter, like the following:

logger.debug("This is a debug message!")

The input parameter msg is a string value and can be a custom message or even the message contents of a caught error or exception.

The logging module also provides a method log(level, msg) that allows us to log a specific log level and message. The level parameter represents the numeric log level to use, and the msg parameter is the string value that will be logged, same as above. We can use the log(level, msg) method like the following:

logger.log(logging.INFO, "This is an info message!")

Instructions

1.

Inside the division function, add an ERROR log message if dividing by 0. The message should read: Attempting to divide by 0!.

Inside the terminal output, type python3 script.py and press Enter. Type the following inputs, when prompted:

  • Dividend = 6
  • Divisor = 0

View the log statements that show in the terminal output. The newly added ERROR log message should appear. To check your answer, click Check Work once your program finishes running.

2.

Use the log(level, msg) method to log a CRITICAL message inside the division function except block. The log message should read No dividend or divisor value entered!.

Inside the terminal output, execute python3 script.py with the following inputs:

  • Dividend = 6
  • Divisor = blank (no value entered)

View the log statements that show in the terminal output. The newly added CRITICAL log message should appear. To check your answer, click Check Work once your program finishes running.

3.

Add an if statement check to see if result is equal to None at the bottom of script.py. If so, log a WARNING with the following message: The result value is None!.

Inside the terminal output, execute python3 script.py with the following inputs:

  • Dividend = 6
  • Divisor = 0

View the log statements that show in the terminal output. The newly added WARNING log message should appear. To check your answer, click Check Work once your program finishes running.

4.

Locate each input statement within the code. After each of these statements, add INFO log messages that show what value was entered.

Inside the terminal output, execute python3 script.py with the following inputs:

  • Dividend = 6
  • Divisor = 3

Execute python3 script.py inside the terminal to run the code with the above inputs. View what log statements show (and do not show) in the terminal output. We will see that no DEBUG or INFO log messages show in our console. Continue to the next exercise to learn why.

Sign up to start coding

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?