January 2019
Beginner
318 pages
8h 23m
English
In this section, we will learn about the logging and warnings modules of Python. The logging module will keep a track of events occurring within a program. The warnings module warns the programmers about the changes made in the language as well as the libraries.
Now, we are going to see a simple logging example. We will write a script called logging_example.py and write the following code in it:
import loggingLOG_FILENAME = 'log.txt'logging.basicConfig(filename=LOG_FILENAME, level=logging.DEBUG,)logging.debug('This message should go to the log file')with open(LOG_FILENAME, 'rt') as f: prg = f.read()print('FILE:')print(prg)
Run the program as follows::
$ python3 logging_example.pyOutput:FILE:DEBUG:root:This ...
Read now
Unlock full access