Python Forum

Full Version: is there any way to hid print()
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a huge code that links with many functions. For debug purposes, I need to have alot
 print() 
in many of my functions. But it makes many of my functions look complicated. Is there any way to have a separate function/script that I can handle all print(), and I can call this function whenever I need to print() contents in all other functions, so that I don't need to write
 print() 
in all current functions and make that look clear when reading it.
PS: it seems to me 'logging' may not be the choice, because I still need to write it in all independent function and that results in the same way as using print() method. But I don't know all features in logging, so if there is a way logging can help, I appreciate for advice. Thank
Logging is really the right way to do this.

import logging

logging.basicConfig(level=logging.DEBUG)
variable = 34
## instead of a debug print(), just use logging.debug.
logging.debug(f"Now the variable is {variable}")
When run this way, you'll get output during the running with the DEBUG output. Change the config line to a higher level (like level=logging.WARNING) and the debug messages go away.

With DEBUG
Output:
$ python3 logit.py DEBUG:root:Now the variable is 34 34
With the level set to WARNING

Output:
$ python3 logit.py 34