Python StdOuts: Don’t Print, Log! | by Normaler Mensch | Towards Data Science

Often on Python, especially as a beginner, you might print( ) a variable in order to see what is happening in your program. It is possible if you rely on too many print statements throughout your program you will face the nightmare of having to comment them all out towards the end. A much more pythonic way to see what the program is doing is logging. You can then limit your prints to command line outputs that the end-user wants to see.
Logging is a comfortable tool to see the status of the program in the development phase. It is ideal if:
You want to differentiate between debug output and program outputYou don’t want irrelevant stdout in your final productYou would like to disable all stdout after development and testYou want to save your program execution history to a file with meta information like time and more detailed debug information.
You want to differentiate between debug output and program output
You don’t want irrelevant stdout in your final product
You would like to disable all stdout after development and test
You want to save your program execution history to a file with meta information like time and more detailed debug information.
You log in python using the logging module.
import logging
The logging module provides some default states of logging various status messages in your program. The default levels are DEBUG, INFO, WARNING, ERROR, and CRITICAL.
If you execute the following program:
import loggingimport ossavepath = ‘path/to/save/at/’if not os.path.exists(savepath):  logging.warning(‘Warning! The savepath provided doesn\’t exist!'                       'Saving at current directory ‘)  savepath = os.getcwd()   logging.info(‘savepath reset at %s’,str(savepath))else:    logging.info('Savepath provided is correct,'                 'saving at %s’,str(savepath))
The path in the example Doesn't exist. You will see the following output:
WARNING:root:warning! The savepath provided doesn’t exist!Saving at current directory
The new current save path information, that is logging.info is not displayed.
This is because the default level of severity of output is “warning”. The order of severity is as follows:
DEBUG
INFO
WARNING (This is default)
ERROR
CRITICAL
If you want to see outputs of a lower severity level, you have to explicitly set them in the logging configuration. Start a new interpreter after this setting, else it won’t work.
logging.basicConfig(level = logging.DEBUG)
Now, everything will be printed out, starting from level debug.
output:
WARNING:root:warning! The savepath provided doesn’t exist! Saving at current directory INFO:root:savepath reset at path/to/current/directory
That is awesome! But for a decent log, we perhaps want to see more information and make it available in a separate file. This can be set using the format and filename in the configuration. Add the desired configuration at the beginning of your file:
logging.basicConfig(filename=’logfilename.log’,level =                     logging.DEBUG,format=’%(asctime)s %                    (message)s’,                    datefmt=’%d/%m/%Y %I:%M:%S %p’)
This logs your information to logfilename.log along with timestamps.
13/06/2020 05:14:47 PM warning! The savepath provided doesn’t exist!                         Saving at current directory 13/06/2020 05:14:47 PM savepath reset at current/working/directory
The next time you run the file, logs get appended to the textfile, so that you have a historic record of your logs in one place. You can also change this and create a new text file each time by adding this to your logging.basicConfig
filemode='w'
You can disable all logging centrally by adding the following to your script, without having to comment each of the messages out:
logger = logging.getLogger()logger.disabled = True
Alternatively, you can choose to change the level of output needed to be written to the log file to only enable warning and critical error messages to the end user.
That was a primer to logging on python. Logging offers much more flexibility than described above (Handlers, filters, changing the default log levels, etc) But this should be enough to start using basic logging and do away with print statements.