Hi! 👋
Currently logging.basicConfig instantiates logging.Formatter as a formatter:
|
fmt = Formatter(fs, dfs, style) |
Now if the developer wants a different formatter class, they have to let logging.basicConfig create the default formatter first and then replace it passing the same parameters:
import logging
class CustomFormatter(logging.Formatter):
... # custom code here
format_ = "......." # arbitrary
logging.basicConfig(format=format_)
formatter = CustomFormatter(fmt=format_)
for handler in logging.root.handlers:
handler.setFormatter(formatter)
A version of that with the blanks filled in can be seen at https://gist.github.com/hartwork/8b5963b5e9a698a3d6d352c657418af3 .
A brittle alternative would be use of inittest.mock.patch:
import logging
from inittest.mock import patch
class CustomFormatter(logging.Formatter):
... # custom code here
with patch("logging.Formatter", CustomFormatter):
logging.basicConfig(format=".......")
None of that is ideal. With a new parameter formatter_class, the code would be written as this:
import logging
class CustomFormatter(logging.Formatter):
... # custom code here
logging.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
Hi! 👋
Currently
logging.basicConfiginstantiateslogging.Formatteras a formatter:cpython/Lib/logging/__init__.py
Line 2111 in 3dfed23
Now if the developer wants a different formatter class, they have to let
logging.basicConfigcreate the default formatter first and then replace it passing the same parameters:A version of that with the blanks filled in can be seen at https://gist.github.com/hartwork/8b5963b5e9a698a3d6d352c657418af3 .
A brittle alternative would be use of
inittest.mock.patch:None of that is ideal. With a new parameter
formatter_class, the code would be written as this:I hope that you are open to this suggestion, and I will open a related pull request in a minute.
Thanks! 🙏
Linked PRs
formattertologging.basicConfig#133578