Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 391
Glemi Avatar asked Sep 06 '25 05:09

Glemi


1 Answers

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.

like image 88
orlp Avatar answered Sep 10 '25 13:09

orlp