← index #979PR #1023
Related · high · value 2.395
QUERY · ISSUE

logging.basicConfig(force=False) on empty handler list never initializes Formatter and Handler

openby nielsson4711opened 2025-02-25updated 2025-02-26

Aiming to add minimalistic logging capabilities soon within starting phase of a micropythion program, one would call:

#minimalistic
logging.basicConfig(stream=stream.sys level=logging.DEBUG)
logger = logging.getLogger('thelog')

Aiming to get a fancy timestamp and information ordering, the call could look like:

# explicit formatting
log_fmt_mipy = "%(name)s - %(levelname)s - %(message)s"
log_fmt_dat_mipy = "%(asctime)s.%(msecs)03d"
logging.basicConfig(stream=stream.sys level=logging.DEBUG)
logger = logging.getLogger('thelog')

The Object "logger" as Logger instance on either case will NEVER get a working Handler and Formatter upon initialization. This is because logging.basicConfig() in (current) line 235 evaluates the list object Logger.handlers instead of the integer number Logger.hasHandlers().

NOTE: Initialization of a date format of type "asctime" within micropython, as shown in the code above, is not (yet) supported (by design) and thus requires explicit implementation efforts.

CANDIDATE · PULL REQUEST

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

openby greezybaconopened 2025-06-11updated 2025-07-31

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.

3 comments
stinos · 2025-06-11

It seems like a small step to just support handlers then, such that the code should remains compatible with CPython?

greezybacon · 2025-06-11

I could do that, yeah. My thought was that for an MCU more than one handler is unlikely, but I agree that compatibility means that everyone feels more comfortable using Micropython.

Josverl · 2025-06-12

I agree that handlers = [] is simpler to learn ,
it also means that adding
Logger.addHandler() makes more sense.

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied