← index #979Issue #691
Related · high · value 1.153
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 · ISSUE

logging.StreamHandler should call super().__init__()

openby uldaopened 2023-06-22updated 2023-08-05

logging.StreamHandler inherits from logging.Handler but does not call super().init()
so it misses to initialize level and formatter instance variables

by the way, logging.Handler should initialize formatter to a working formatter, not None.
e.g. if you create and add a new StreamHandler, it has no formatter set by default and any logging will generate an exception.

1 comment
colin-nolan · 2023-08-05

I also encountered this issue, and have added a PR to fix in #710.

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