Minor: Logging module debugging improvement

I want to log this as a minor feature request. Posting here for advice first.
Thank you!

Feature or enhancement

I suggest that when the logging module attaches a default StreamHandler to the root logger, that it also emits a DEBUG log message indicating that this has happened.

Pitch

I have a complex use case where I need to manipulate multiple loggers, specifically I have application-specific loggers configured with their handlers and then at another step in my application I need to reconfigure which logger and handlers are being used.

To achieve this I remove and reattach various log handlers, to various loggers, at run time.

To further complicate things, I have some log handlers that emit messages to the user’s terminal (appearing like the output of a StreamHandler) with some additional formatting going on. Some of these loggers come from imported libraries so I can’t easily change how they work.

However, due to a bug in my application, at one point my code was not attaching any handlers to the loggers.

I believe this caused Python logging to fallback and emit logs via the root logger and automatically attach a StreamHandler to emit those logs. Remember that I had explicitly removed all handlers at the beginning of my process, because I need my customised logging instead.

This produced (in my case) undesirable output to the stream. It was difficult for me to debug the source of these messages.

I first thought that one of my logging handlers was emitting the logs to my terminal. It took some time to step through the code and debug the logging configurations, handlers etc and come to the conclusion that all of my logging handlers were actually correct - just that in some scenarios they were not being attached and this caused messages to be emitted by some handler I had not attached directly by myself.

I realise the default behaviour is intended, and helpful to the vast majority of users:

  • Without any handlers, logging would be mostly useless as nothing would be emitted
  • With a stream handler being automatically attached there is hopefully direct feedback to the user that logging is happening
  • Many existing applications probably rely on this and other nuanced functionality today due to Python logging being a core functionality. I don’t think a WARNING should be emitted if there are no handlers attached as it may be too noisy.
  • My use case is overcomplicated compared to the average user requirements and the root cause of my issue is a bug in my code rather than in the logging module.

I therefore don’t want to propose material changes to the existing behavior (emit to stream when there are no handlers for the root logger).

Instead, I propose that a DEBUG message is emitted when no handlers were configured to indicate where the logging is coming from.

Something like this:

DEBUG: No handlers were attached to the root logger, fallback logging module behavior is to attach a new StreamHandler / emit to stderr / .