Skip to content

Bugfixes and enhancements to logging module - #1179

Open
jamesquilty wants to merge 6 commits into
micropython:masterfrom
jamesquilty:fix/python-stdlib-logging
Open

jamesquilty wants to merge 6 commits into
micropython:masterfrom
jamesquilty:fix/python-stdlib-logging

Conversation

@jamesquilty

Copy link
Copy Markdown

Summary

This PR introduces the following changes:

  1. Fixes a bug in shutdown() preventing removal of loggers
    shutdown function of logging module does not empty _loggers #928.
  2. Refactors basicConfig() to clarify it configures the root logger
    logging.basicConfig(force=False) on empty handler list never initializes Formatter and Handler #979.
  3. Fixes a bug in basicConfig() where the root handler level is set.
  4. Refactors Formatter.format() to use record.__dict__.
  5. Adds a feature allowing logger output to be disabled by setting an attribute.
  6. Updates the module version number.

Each change is made via one commit and details may be found in commit message bodies. Complex changes not described in a prior issue follow.

3. Fix for basicConfig() setting the root handler level

Named loggers without handlers fall-back to using the root handler which should be NOTSET by default. The logging module 0.6.2 sets the root logger's level, with the consequence that if the named logger's level is set lower than the root handler's level then messages which would be expected to be logged by the named logger are unexpectedly dropped by the root handler. The fix is to set the root logger level to NOTSET by default [this is also the CPython default].

5. Allow logger output to be disabled by setting an attribute

This commit provides feature parity with the CPython standard library, which implements logger disabling via an attribute logger.disabled.

This feature does not change the existing behaviour: loggers emit messages normally by default. Loggers can be disabled dynamically by the statement logger.disabled = True which disables logger output by making the isEnabled() method return False, silently bypassing the logger's handlers. Output is restored by the statement logger.disabled = False

Disabling logger output has a number of use cases, it is particularly useful for temporarily disabling logger output in unit tests where the log messages can become more of a hindrance than a help.

Testing

Manual unit testing on the REPL and casual integration testing in an existing code has been performed on the ESP32 port.

Generative AI

I did not use generative AI tools when creating this PR.

James Quilty added 6 commits November 22, 2026 11:07
Fixes issue micropython#928.

Signed-off-by: James Quilty <James.Quilty@vuw.ac.nz>
The root logger's handler level should be NOTSET (the default) because
named loggers which don't have handlers fall-back to using the root
handler. If the named logger's level is set lower than the root
handler's level then messages which would be expected to be logged by
the named logger are unexpectedly dropped by the root handler.

Signed-off-by: James Quilty <James.Quilty@vuw.ac.nz>
Signed-off-by: James Quilty <James.Quilty@vuw.ac.nz>
Fixes micropython#979.

Signed-off-by: James Quilty <James.Quilty@vuw.ac.nz>
The "disabled" attribute defaults to False: loggers emit messages normally.
When set with 'logger.disabled = True', this attribute disables logger
output by making the isEnabled() method return False, silently bypassing
the logger's handlers. Disabling logger output has a number of use cases,
it is particularly useful when running unit tests where the log output can
become more of a hindrance than a help.

This commit provides feature parity with the CPython standard library,
which implements logger disabling in the same manner.

Signed-off-by: James Quilty <James.Quilty@vuw.ac.nz>
The changes introduce backward-compatible bug fixes and functionality,
therefore the minor version number should be increased under semantic
versioning rules.

Signed-off-by: James Quilty <James.Quilty@vuw.ac.nz>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant