Glemi
Glemi

Reputation: 745

Why do Python docs recommend against using logging for normal console output?

In the Python docs on logging it says "the best tool for the task" of "Display[ing] console output for ordinary usage of a command line script or program" is to use the print() function.

I don't understand why. What would be the reasoning behind that?

I want to use logging systematically in my project, which is a command line tool with some standard output to the console. All standard output normally generated by print() should also go to the log file.

For my root logger I use both a StreamHandler for console output and a FileHandler to generate a log file. If I want to follow the recommendation I have to either duplicate every print-statement with an identical logging-statement or find a way to redirect the output from print() as suggested in this question. The accepted answer there was to replace the print function by writing print = log.info, which effectively amounts to using logging for ordinary console output after all.

Upvotes: 4

Views: 1930

Answers (3)

FeRD
FeRD

Reputation: 2104

I really feel like you were reading too much into that admonishment in the documentation. Or, to look at it another way, I think you were giving the Python community too much credit for not needing to be told they shouldn't do stupid things.

When they say this:

Task you want to perform: Display console output for ordinary usage of a command line script or program
The best tool for the task: print()

What they mean is, don't do idiot things like this:

  1. Write a Python implementation of grep that uses logging.log() to display matching lines
  2. Write a git log that displays the commit history as a series of log() messages
  3. Reimplement units in a way that log()s the result of the conversion

Because, people absolutely would! And probably have, despite that friendly discouragement. Just... hopefully less often than they would have otherwise.

If your program's output really is logging, and not the results of the program run, then by all means, use logging! You're not one of the people they're talking to, with that advice.

Upvotes: 2

orlp
orlp

Reputation: 117771

If you want to log something, you want to log something. If you want to output data on stdout, you want to print something. Sometimes one masquerades as the other, but they are fundamentally different.

If you are creating a command line application that reads from stdin and outputs to stdout, you are printing. If you are writing information, warning or error updates about some process going on, you are logging.

I want to use logging systematically in my project

Then log systematically, instead of printing systematically.

All standard output normally generated by print() should also go to the log file.

Then those shouldn't be print calls, but log calls instead.

In my opinion the question you linked contains bad advice. You should use regular logging calls and set up your logger such that it outputs both to the console as well as a log file.

Upvotes: 3

MyNameIsCaleb
MyNameIsCaleb

Reputation: 4489

Logging is more resource intensive and time consuming than simple printing. If all you want to do is get the console output, use print. However, you want to also log to a file all of your outputs, which is why logging is what you want to use.

If you want to do both print to console and save to log, you could put both commands in each time you want to do this, create a custom function to do both things separately, or create a StreamHandler and a FileHandler like you alluded to to send your logs out to stdout. You can follow this answer for an example.

Upvotes: 3

Related Questions