Creating a Simple Logger

We'll cover the following...

Creating a log with the logging module is easy and straight-forward. It’s easiest to just look at a piece of code and then explain it, so here’s some code for you to read:

Press + to interact
import logging
# add filemode="w" to overwrite
logging.basicConfig(filename="sample.log", level=logging.INFO)
logging.debug("This is a debug message")
logging.info("Informational message")
logging.error("An error has happened!")
# Let's use our file reading knowledge to
# read the log file
with open("sample.log") as file_handler:
for line in file_handler:
print(line)

As ...

Access this course and 1400+ top-rated courses and projects.