Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does python logging replace print?

Tags:

python

logging

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?

like image 414
Merchako Avatar asked Oct 30 '14 16:10

Merchako


2 Answers

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.

like image 185
chepner Avatar answered Oct 18 '22 07:10

chepner


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.

like image 43
Klaus D. Avatar answered Oct 18 '22 09:10

Klaus D.