Up to now, I've been peppering my code with 'if VERBOSE: print debug message
'. Then I learned how to use the logging
module. It looks as though it does everything I could possibly want, and then some. So much so that I got carried away and replaced all print
statements in my code with logging
statements, not just the verbose ones.
Is this a mistake? If I'm already using logging
, can I use it to replace print
in every way? If not, how do I choose whether to use logging
or print
?
print
would still be used for output that is essential to the operation of your program. logging
is for output that might be useful in determining how your program is working (errors, progress, etc), but could be omitted without affecting the usability of your program.
The Basic Logging Tutorial discusses when to use logging
versus other mechanisms.
Python logging can completely replace your print statements. You just have more options. The default configuration of logging writes to the console as print does, but you can use logging in other ways, like writing to a logging server, reformatting the output, adding information like the module the line came from, or the time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With