logging: Add `handler` param to `basicConfig`.

CPython allows specifying a list of handlers in the initialization of
logging with basicConfig(handlers=<iterable>). This adds similar support
but only with a single handler. It allows to initialize logging with a
single, specialized handler.

Signed-off-by: Jared Hancock <jared@greezybacon.me>
pull/1023/head
Jared Hancock 2025-06-11 07:35:43 -05:00
rodzic 6e24cffe95
commit 9d4e1ae34d
1 zmienionych plików z 9 dodań i 4 usunięć

Wyświetl plik

@ -223,6 +223,7 @@ def basicConfig(
format=None,
datefmt=None,
level=WARNING,
handler=None,
stream=None,
encoding="UTF-8",
force=False,
@ -237,10 +238,14 @@ def basicConfig(
h.close()
logger.handlers = []
if handler is not None:
if filename is None:
handler = StreamHandler(stream)
else:
handler = FileHandler(filename, filemode, encoding)
elif stream or filename:
raise ValueError("'stream' or 'filename' should not be "
"specified together with 'handler'")
handler.setLevel(level)
handler.setFormatter(Formatter(format, datefmt))