Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
Description
Hi! 👋
Currentlylogging.basicConfig instantiateslogging.Formatter as a formatter:
cpython/Lib/logging/__init__.py
Line 2111 in3dfed23
| fmt=Formatter(fs,dfs,style) |
Now if the developer wants a different formatter class, they have to letlogging.basicConfig create the default formatter first and then replace it passing the same parameters:
importloggingclassCustomFormatter(logging.Formatter): ...# custom code hereformat_="......."# arbitrarylogging.basicConfig(format=format_)formatter=CustomFormatter(fmt=format_)forhandlerinlogging.root.handlers:handler.setFormatter(formatter)
A version of that with the blanks filled in can be seen athttps://gist.github.com/hartwork/8b5963b5e9a698a3d6d352c657418af3 .
A brittle alternative would be use ofinittest.mock.patch:
importloggingfrominittest.mockimportpatchclassCustomFormatter(logging.Formatter): ...# custom code herewithpatch("logging.Formatter",CustomFormatter):logging.basicConfig(format=".......")
None of that is ideal. With a new parameterformatter_class, the code would be written as this:
importloggingclassCustomFormatter(logging.Formatter): ...# custom code herelogging.basicConfig(format=".......",formatter_class=CustomFormatter)
I hope that you are open to this suggestion, and I will open a related pull request in a minute.
Thanks! 🙏
Linked PRs
Metadata
Metadata
Assignees
Labels
Projects
Status