This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Better logging defaults
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.5
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: ArneBab, r.david.murray, vinay.sajip
Priority: normal Keywords:

Created on 2014-01-22 14:25 by ArneBab, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (8)
msg208812 - (view) Author: Arne Babenhauserheide (ArneBab) * Date: 2014-01-22 14:25
Currently the default operation of logging prints messages like the following:

INFO:root: Milk found.

This is close to a print-call, but not much more useful - especially not in small scripts. To make the default settings more useful to quick scripts, I would suggest adding the filename and linenumber by default. This would be equivalent to the following setup:

logging.basicConfig(
    level=logging.INFO, 
    format="%(levelname)s (%(filename)s::%(lineno)s): %(message)s")

With that setup, the above message would look like this:

INFO (move.py::23): Milk found.
msg208816 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-01-22 14:52
Well, you can always call basicConfig yourself...that's kind of the point of it.  I'm guessing the current output was chosen exactly because it is pretty much just a print of the input to the log method.  But I don't have strong feelings about it.  On the other hand, I would imagine that there are backward compatibility concerns: there may well be people who parse the logging output of their programs whose parsers would be broken if the default logging format changes.  I'm not saying that is an overriding concern, just that it is a concern.
msg208843 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2014-01-22 19:43
I don't think it is necessary or advisable to change the default, as it is very easy to provide your own format. As RDM has noted, this could affect backward compatibility. Furthermore, there are bound to be differing views of what an ideal default should be, so we might as well leave this bikeshed un-repainted ;-)
msg208882 - (view) Author: Arne Babenhauserheide (ArneBab) * Date: 2014-01-23 07:37
I do not think it is necessary to change the defaults, but I do think that it would help users of python.

In the usecase of bigger applications, the default logging format does not matter: the overhead of logging.basicConfig() only happens once while the advantages help for the whole time.

In the usecase of small programs and quick scripts, the overhead of having to use logging.basicConfig() is big, though: It requires reading up on the different formatting options and actually thinking about a good format. But the results only help that small script, and for the next script the programmer will likely have to read the documentation again. If logging would have a better default, the programmer would just have to remember “import logging, then call logging.LEVEL()”. In the python console, logging.<tab> will autocomplete the level, so programmers do not actually have to read up anything: It actually goes down to “log with logging”. And that’s a very different level of usability than having to call logging.basicConfig().

And I think that the filename and the line number is the minimum information necessary for any debug message - though the format could be matched to `grep -n` by replacing the double colon with a single colon.

To stay with your analogy: It’s not about the color of the bikeshed. It’s about providing the basic bycycle stands in the shed, so people who have no strong feelings about bikesheds can simply go there, leave the bike and move on - instead of having to adjust the stand every time to keep their bike from falling over.

I agree, though, that backwards compatibility could be an issue. Can you give me a hint, how I could investigate that?
msg208914 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2014-01-23 11:38
> Can you give me a hint, how I could investigate that?

There's no easy way - a lot of Python source is not available for public view, so there's no telling what the impact would be. While the use of the module-level functions is provided as a convenience, I don't feel that the need to call basicConfig () is problematic, even for short scripts.
msg208936 - (view) Author: Arne Babenhauserheide (ArneBab) * Date: 2014-01-23 14:30
Just calling basicConfig() is not problematic.

Specifying the format is what requires mental investment (because for that I need to read up on the config options).

Would it be possible to add a simple way to select a certain logging style?

In Mercurial I can for example call `hg log --style changelog` to get GNU-style changelog output. This is easy to remember. On the other hand, getting --template "string" right for somewhat complex log styles is a small task in itself.
msg208937 - (view) Author: Arne Babenhauserheide (ArneBab) * Date: 2014-01-23 14:31
(though calling logging.basicConfig() still produces some overhead, it does not pull me away from the task I’m currently doing)
msg208963 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2014-01-23 15:57
> Specifying the format is what requires mental investment (because for that I need to read up on the config options).

Not sure what you mean by config options. Is it too much to remember the format= parameter in your first post?

I'm not planning to change the default because of already stated backward compatibility concerns, as it's easy enough to specify what you want, and it's not as if this type of request has come from lots of people over the years that logging has been in use (basicConfig has been there in its present form since Python 2.4).
History
Date User Action Args
2022-04-11 14:57:57adminsetgithub: 64544
2014-02-05 17:38:51vinay.sajipsetstatus: pending -> closed
2014-01-23 15:57:24vinay.sajipsetstatus: open -> pending

messages: + msg208963
2014-01-23 14:31:48ArneBabsetmessages: + msg208937
2014-01-23 14:30:14ArneBabsetstatus: pending -> open

messages: + msg208936
2014-01-23 11:38:26vinay.sajipsetstatus: open -> pending

messages: + msg208914
2014-01-23 07:37:40ArneBabsetstatus: pending -> open

messages: + msg208882
2014-01-22 19:43:38vinay.sajipsetstatus: open -> pending
type: enhancement
resolution: wont fix
versions: + Python 3.5
2014-01-22 19:43:03vinay.sajipsetmessages: + msg208843
2014-01-22 14:52:46r.david.murraysetnosy: + vinay.sajip, r.david.murray
messages: + msg208816
2014-01-22 14:25:20ArneBabcreate