Skip to content

Commit 25185a5

Browse files
committed
logging: Add handlers param to basicConfig.
CPython allows specifying a list of handlers in the initialization of logging with basicConfig(handlers=<iterable>). This adds similar support to Micropython. It allows to initialize logging with a set of specialized handlers. Signed-off-by: Jared Hancock <[email protected]>
1 parent 6e24cff commit 25185a5

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

python-stdlib/logging/logging.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ def basicConfig(
223223
format=None,
224224
datefmt=None,
225225
level=WARNING,
226+
handlers=(),
226227
stream=None,
227228
encoding="UTF-8",
228229
force=False,
@@ -235,19 +236,24 @@ def basicConfig(
235236
if force or not logger.handlers:
236237
for h in logger.handlers:
237238
h.close()
238-
logger.handlers = []
239+
logger.handlers = list(handlers)
239240

240-
if filename is None:
241-
handler = StreamHandler(stream)
242-
else:
243-
handler = FileHandler(filename, filemode, encoding)
241+
if handlers is None:
242+
if filename is None:
243+
handler = StreamHandler(stream)
244+
else:
245+
handler = FileHandler(filename, filemode, encoding)
246+
elif stream or filename:
247+
raise ValueError("'stream' or 'filename' should not be "
248+
"specified together with 'handlers'")
244249

245-
handler.setLevel(level)
246-
handler.setFormatter(Formatter(format, datefmt))
250+
for handler in logging.handlers:
251+
handler.setLevel(level)
252+
handler.setFormatter(Formatter(format, datefmt))
247253

248-
logger.setLevel(level)
249-
logger.addHandler(handler)
254+
logger.addHandler(handler)
250255

256+
logger.setLevel(level)
251257

252258
if hasattr(sys, "atexit"):
253259
sys.atexit(shutdown)

0 commit comments

Comments
 (0)