From 9d4e1ae34d0dc471f5a9b81114e25fe788bd8ef3 Mon Sep 17 00:00:00 2001 From: Jared Hancock Date: Wed, 11 Jun 2025 07:35:43 -0500 Subject: [PATCH] logging: Add `handler` param to `basicConfig`. CPython allows specifying a list of handlers in the initialization of logging with basicConfig(handlers=). 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 --- python-stdlib/logging/logging.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/python-stdlib/logging/logging.py b/python-stdlib/logging/logging.py index 551bf7152..256760171 100644 --- a/python-stdlib/logging/logging.py +++ b/python-stdlib/logging/logging.py @@ -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 filename is None: - handler = StreamHandler(stream) - else: - handler = FileHandler(filename, filemode, encoding) + 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))