How do you log in your projects?

Martin Di Paola martinp.dipaola at gmail.com
Thu Feb 10 23:21:29 EST 2022


>? Logs are not intended to be read by end users. Logs are primarily
>used to understand what the code is doing in a production environment.
>They could also be used to gather metrics data.
>
>Why should you log to give a message instead of simply using a print?

You are assuming that logs and prints are different but they aren't. It 
is the same technology: some string formatted in a particular way sent 
to some place (console or file generally).

But using the logging machinery instead a plain print() you win a few 
features like thread safety and log levels (think in an user that wants 
to increase the verbose level of the output)

When communicating with an user, the logs that are intended to him/her 
can be sent to the console (like a print) in addition to a file.

For user's perspective, they look just like a print.

>Why? Traceback is vital to understand what and where the problem is. I
>think you're confusing logs with messages. The stack trace can be
>logged (I would say must), but the end user generally sees a vague
>message with some hints, unless the program is used internally only.

Yes, that's exactly why the traceback is hidden by default because the 
user don't care about it. If the error is due something that the user 
did wrong, then the message should say that and, if possible, add 
a suggestion of how to do it.

For example "The file 'foo.md' was not found." is quite descriptive. If 
you add to that message a traceback, that will just clutter the console.

Tracebacks and other errors and warnings must be logged in a file.  
I totally agree with that. Specially true when we are talking with 
server-like software.

Tracebacks can be printed to the user if a more verbose output is 
enabled. In that case you could even pretty print the traceback with 
syntax highlighting.

I guess that this should be discussed case by case. May be you are 
thinking more in a case where you have a service running and logging and 
I am more thinking in a program that a human executes by hand.

What info and how is presented to the user changes quite a bit.
>-- https://mail.python.org/mailman/listinfo/python-list


More information about the Python-list mailing list