Lazo Lab Sign up

Ship It: Testing, Git & Sharing Your Apps / Make it solid

Lesson 5 of 10

Logging instead of print

For bigger apps, the logging module records what happened with levels like INFO and ERROR, and can write to a file.

Key points

  • logging.basicConfig(level=logging.INFO)
  • log.info, log.warning, log.error
  • Never log passwords or tokens
import logging
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
log = logging.getLogger("weather")
log.info("Fetching weather for %s", "Kuwait City")

Your turn

Replace the debug prints in your weather app with logging.

Watch a video on thisOpens YouTube search results for “Logging instead of print” in a new tab

Quiz · +10 XP

What should never be written to logs?

Log in to save progress and earn XP.