• 8 hours
  • Medium

Free online content available in this course.

course.header.alt.is_video

course.header.alt.is_certifying

Got it!

Last updated on 8/30/22

Use the Pdb and Logging Modules

Use the Python Debugger

The Python debugger, or pdb, is a standard Python library module. It allows you to create breakpoints in a program.

A demonstration is worth a thousand words, so in the following video, we’ll look at the basic features of pdb to find the source of bugs in our project. 

You can download the code from the screencast here

Create Personalized Logs

I’m sure you’re now grateful to Python for the messages it sends you! However, so far, they have mainly been to do with errors. Wouldn’t it be handy to display other types of messages, too? You could list and show them as you move through the program to provide helpful information.

That function is called logging, and that’s what the logging module is there for! Like pdb, it’s part of the standard Python library. It provides an insight into how a program works and displays messages ranked by severity level

To use it, you first need to import the module.

import logging

Then, you need to know how to use the different levels of severity. They range from the lowest to the most critical:

  • logging.debug()  : Low-level information on an instruction, usually used by the programmer to help fix bugs when they appear.

  • logging.info()  : High-level information on a program, for example, to confirm that everything is working as it should.

  • logging.warning)  : Something unexpected has happened, although the program can continue working. It might also be an indication of imminent risk.

  • logging.error()  : An error has occurred, probably because an exception was raised. The program won’t work as it’s supposed to.

  • logging.critical()  : A serious problem has caused the program to stop running.

All these logs display a message in the console, but it’s important to use the right levels. For example, let’s see what would happen if we put several logs one after the other like this:

import logging

logging.info("Information on the functioning of a program")

logging.debug("Detailed information on the functioning of a program")

logging.warning("Something unexpected occurred")

In this case, the console will only display the warning. This is because if the logging level is not specified, the method  logging.warning()  will take priority. To specify the log level, you need to use the code below in front of your log messages:

logging.basicConfig(level=logging.DEBUG)

Let’s Recap!

  • The pdb module pauses the program and opens an interactive console for debugging the program.

  • The logging module displays personalized messages at different severity levels in the console. These messages provide precise information for debugging the program.

In the next chapter, you’ll see how to set up your IDE and make it your best ally for debugging. Follow me!

Example of certificate of achievement
Example of certificate of achievement