- On a line per line basis? on a function/method basis?

In general I prefer logging line by line instead per function.

It is easy to add a bunch of decorators to the functions and get the logs of all the program but I most of the time I end up with very confusing logs.

There are exceptions, yes, but I prefer the line by line where the log should explain what is doing the code.

- Which kind of variable contents do you write into your logfiles?
- How do you decide, which kind of log message goes into which level?
- How do you prevent logging cluttering your actual code?

These three comes to the same answer: I think on whom is going to read the logs.

If the logs are meant to be read by my users I log high level messages,
specially before parts that can take a while (like the classic "Loading...").

If I log variables, those must be the ones set by the users so he/she can understand how he/she is controlling the behaviour of the program.

For exceptions I print the message but not the traceback. Across the code tag some important functions to put an extra message that will enhance the final message printed to the user.

https://github.com/byexamples/byexample/blob/master/byexample/common.py#L192-L238

For example:

    for example in examples:
        with enhance_exceptions(example, ...):
            foo()

So if an exception is raised by foo(), enhance_exceptions() will attach to it useful information for the user from the example variable.

In the main, then I do the pretty print
https://github.com/byexamples/byexample/blob/master/byexample/byexample.py#L17-L22

If the user of the logs is me or any other developer I write more debugging 
stuff.

My approach is to not log anything and when I have to debug something I use a debugger + some prints. When the issue is fixed I review which prints would be super useful and I turn them into logs and the rest is deleted.


On Tue, Feb 08, 2022 at 09:40:07PM +0100, Marco Sulla wrote:
These are a lot of questions. I hope we're not off topic.
I don't know if mine are best practices. I can tell what I try to do.

On Tue, 8 Feb 2022 at 15:15, Lars Liedtke <lied...@punkt.de> wrote:
- On a line per line basis? on a function/method basis?

I usually log the start and end of functions. I could also log inside
a branch or in other parts of the function/method.

- Do you use decorators to mark beginnings and ends of methods/functions
in log files?

No, since I put the function parameters in the first log. But I think
that such a decorator it's not bad.

- Which kind of variable contents do you write into your logfiles? Of
course you shouldn't leak secrets...

Well, all the data that is useful to understand what the code is
doing. It's better to repeat the essential data to identify a specific
call in all the logs of the function, so if it is called
simultaneously by more clients you can distinguish them

- How do you decide, which kind of log message goes into which level?

It depends on the importance, the verbosity and the occurrences of the logs.

- How do you prevent logging cluttering your actual code?

I have the opposite problem, I should log more. So I can't answer your question.
--
https://mail.python.org/mailman/listinfo/python-list
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to