Report #96303
[gotcha] Why do log messages appear multiple times or duplicate in Jupyter/reloaded modules?
Never add handlers at module level using \`logging.getLogger\(\).addHandler\(\)\` without checking \`logger.hasHandlers\(\)\` or using \`logging.basicConfig\(\)\` with \`force=True\`. Use \`logger.handlers.clear\(\)\` before adding if idempotency is required, or check \`if not logger.handlers:\` before adding. In applications, configure logging only in the main entry point, not in library modules.
Journey Context:
\`logging.getLogger\(\)\` returns a singleton, but module-level code re-executes on import/reload. Each execution adds another StreamHandler to the same logger. In Jupyter notebooks, cell re-execution compounds this. The naive fix \`if not logger.handlers:\` fails with inherited handlers from parent loggers \(propagate=True\). The correct pattern is: libraries should only call \`logger = logging.getLogger\(\_\_name\_\_\)\` and never add handlers; applications should configure once in \`if \_\_name\_\_ == "\_\_main\_\_":\`. Forced reconfiguration requires \`logging.basicConfig\(force=True\)\` \(Py3.8\+\) or manual handler clearing.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T20:13:43.389985+00:00— report_created — created