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: Implement `__repr__` methods for logging.Logger and others
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: vinay.sajip Nosy List: cool-RR, felixonmars, lilydjwg, python-dev, vinay.sajip
Priority: normal Keywords: patch

Created on 2016-07-29 20:09 by cool-RR, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
patch-1.diff vinay.sajip, 2016-08-03 15:38 First patch review
Messages (9)
msg271646 - (view) Author: Ram Rachum (cool-RR) * Date: 2016-07-29 20:09
A while ago I was debugging through code that uses the `logging` module, and I was looking at `Logger` objects and possibly other kinds of objects (maybe handlers? It was a while ago.)

I was frustrated because I was trying to figure out what a particular logger was, but typing `logger` in the shell just showed the opaque `<logging.Logger object at 0x0000000002293550>`. I would like to know the name of the logger without bothering to do `logger.name`.

I suggest that we do a run over the `logging` module and see which classes could have a helpful `__repr__` added to them. I might have time to do this, but first I want to know whether there is a general agreement that such a patch would be welcome.
msg271912 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2016-08-03 15:38
See attached patch for proposed changes.
msg272079 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-08-06 09:28
New changeset 6c304a2ca139 by Vinay Sajip in branch 'default':
Closes #27650: Implemented repr methods for logging objects.
https://hg.python.org/cpython/rev/6c304a2ca139
msg272094 - (view) Author: Ram Rachum (cool-RR) * Date: 2016-08-06 11:37
Vinay: Looks good to me. What do you think about adding tests?
msg272095 - (view) Author: Ram Rachum (cool-RR) * Date: 2016-08-06 11:38
If you want, I can implement them.
msg272099 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2016-08-06 16:24
IMO it's not really worth adding tests for __repr__() - because no external code should depend on the exact representation of instances of these kinds of classes.
msg272100 - (view) Author: Ram Rachum (cool-RR) * Date: 2016-08-06 16:30
Okay, but consider that if there are no tests, this code will never be run during testing. Consider that if there's a bug in that code that causes an exception to be raised (unlikely, I know, but maybe from the call to getLevelName) it won't be discovered until a release of Python is made and some user runs it and sees an exception. Maybe someone in the future will want to add more code into the __repr__ and that code will raise an exception. Having tests that run these methods ensures this will be caught before release.
msg284090 - (view) Author: lilydjwg (lilydjwg) * Date: 2016-12-27 12:43
This hangs this test from celery: https://github.com/celery/celery/blob/master/t/unit/app/test_log.py#L72

It constructs a Logger with `.parent` to itself, making getEffectiveLevel loop forever. The code tries to raise an exception when such loops are detected, but its message contains the logger's repr which calls getEffectiveLevel.
msg284094 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2016-12-27 13:18
This sounds like a Celery bug, so I don't propose to change anything in this area.
History
Date User Action Args
2022-04-11 14:58:34adminsetgithub: 71837
2016-12-27 13:18:18vinay.sajipsetmessages: + msg284094
2016-12-27 12:52:02felixonmarssetnosy: + felixonmars
2016-12-27 12:43:58lilydjwgsetnosy: + lilydjwg
messages: + msg284090
2016-08-06 16:30:25cool-RRsetmessages: + msg272100
2016-08-06 16:24:04vinay.sajipsetmessages: + msg272099
2016-08-06 11:38:21cool-RRsetmessages: + msg272095
2016-08-06 11:37:31cool-RRsetmessages: + msg272094
2016-08-06 09:28:49python-devsetstatus: open -> closed

nosy: + python-dev
messages: + msg272079

resolution: fixed
stage: patch review -> resolved
2016-08-03 15:38:33vinay.sajipsetfiles: + patch-1.diff
messages: + msg271912

assignee: vinay.sajip
keywords: + patch
stage: patch review
2016-07-29 20:09:25cool-RRcreate