Skip to content

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

Open

Description

@nielsson4711

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.

Activity

  1. jamesquilty commented on Oct 21, 2026

    @jamesquilty

    I took a look at this while fixing some other bugs and and found that logging.basicConfig() creates and configures only the root logger (i.e. a logger named "root"). This is the same behaviour as the CPython standard library, described in the documentation and source code. I think this could be made a bit clearer in the MicroPython logging.basicConfig() function by:

    • changing the variable name from generic (logger) to specific (root) which aligns with the CPython library;
    • changing the test on line 235, as you suggest, to read if force or not logger.hasHandlers():.

    I'll add these changes to the fixes I'm making.

    The time.strftime() function which Formatters require to process "asctime" is, in fact, implemented in micropython-lib/python-stdlib/time and the time module is de facto a requirement of the logging module. It's been noted in another Issue that perhaps this needs to be a little better documented.

  2. added a commit that references this issue on Oct 21, 2026
    c7d28f6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions