pdf&rendition=1-3
pdf&rendition=1-3
Logging is a great way to understand what’s happening in your program and in what order it’s
happening
To enable the logging module to display log messages on your screen as your program runs, copy
the following to the top of your program
when Python logs an event, it creates a LogRecord object that holds information about that event
The logging module’s basicConfig() function lets you specify what details about
the LogRecord object you want to see and how you want those details displayed.
Here, we use the logging.debug() function when we want to print log information
This debug() function will call basicConfig(), and a line of information will be printed.
The print(factorial(5)) call is part of the original program, so the result is displayed even if logging
messages are disabled.
But the log messages displayed by logging.debug() show that the i variable is starting at 0 instead
of 1.
Change the for i in range(n + 1): line to for i in range(1, n + 1):, and run the program again. The
output will look like this:
Logging Levels
Logging levels provide a way to categorize your log messages by importance.
There are five logging levels, described in Table 11-1 from least to most important.