hartwork ยท GitHub

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

Read the original on github.com โ†—