In python, why use logging instead of print?

Viewed 57312

For simple debugging in a complex project is there a reason to use the python logger instead of print? What about other use-cases? Is there an accepted best use-case for each (especially when you're only looking for stdout)?

I've always heard that this is a "best practice" but I haven't been able to figure out why.

7 Answers

In brief, the advantages of using logging libraries do outweigh print as below reasons:

  • Control what’s emitted
  • Define what types of information you want to include in your logs
  • Configure how it looks when it’s emitted
  • Most importantly, set the destination for your logs

In detail, segmenting log events by severity level is a good way to sift through which log messages may be most relevant at a given time. A log event’s severity level also gives you an indication of how worried you should be when you see a particular message. For instance, dividing logging type to debug, info, warning, critical, and error. Timing can be everything when you’re trying to understand what went wrong with an application. You want to know the answers to questions like:

  • “Was this happening before or after my database connection died?”
  • “Exactly when did that request come in?”

Furthermore, it is easy to see where a log has occurred through line number and filename or method name even in which thread.


Here's a functional logging library for Python named loguru.

Logging essentially creates a searchable plain text database of print outputs with other meta data (timestamp, loglevel, line number, process etc.).

This is pure gold, I can run egrep over the log file after the python script has run. I can tune my egrep pattern search to pick exactly what I am interested in and ignore the rest. This reduction of cognitive load and freedom to pick my egrep pattern later on by trial and error is the key benefit for me.

tail -f mylogfile.log | egrep "key_word1|key_word2"

Now throw in other cool things that print can't do (sending to socket, setting debug levels, logrotate, adding meta data etc.), you have every reason to prefer logging over plain print statements.

I tend to use print statements because it's lazy and easy, adding logging needs some boiler plate code, hey we have yasnippets (emacs) and ultisnips (vim) and other templating tools, so why give up logging for plain print statements!?

I would add to all other mentionned advantages that the print function in standard configuration is buffered. The flush may occure only at the end of the current block (the one where the print is). This is true for any program launched in a non interactive shell (codebuild, gitlab-ci for instance) or whose output is redirected.

If for any reason the program is killed (kill -9, hard reset of the computer, …), you may be missing some line of logs if you used print for the same.

However, the logging library will ensure to flush the logs printed to stderr and stdout immediately at any call.

Related