fix(core): stop hijacking stdout logging on import - #6341
rsareddy0329 wants to merge 1 commit into
Conversation
get_sagemaker_config_logger() attached a StreamHandler(sys.stdout) to the "sagemaker.config" logger and set propagate=False the first time it ran (which happens during config resolution at import). A library forcing its own stdout handler and disabling propagation overrides the application's logging configuration and pollutes stdout just by importing sagemaker. Follow the standard library-logging pattern instead: - Install a NullHandler on the top-level "sagemaker" logger at import so records are safely discarded until the application configures logging. - Have get_sagemaker_config_logger() only set a default INFO level (when unset) and let records propagate to the application's handlers, rather than attaching a stdout handler or disabling propagation. Config-substitution messages are still emitted on the "sagemaker.config" logger; applications that configure logging (e.g. logging.basicConfig) receive them through their own handlers and formatting. Closes aws#4387
|
Claude finished @rsareddy0329's task in 2m 16s —— View job Code Review: fix(core): stop hijacking stdout logging on import
The core change is right: replacing the stdout However, I think the fix is incomplete and, as written, may not actually stop the reported stdout output on 🔴 Main concern:
|
Issue #, if available: Closes #4387
Description of changes:
get_sagemaker_config_logger()attached aStreamHandler(sys.stdout)to thesagemaker.configlogger and setpropagate=Falsethe first time it ran (whichhappens during config resolution at import). A library forcing its own stdout
handler and disabling propagation overrides the application's logging
configuration and writes to stdout just from
import sagemaker, as reported in#4387.
This follows the standard library-logging pattern instead:
NullHandleron the top-levelsagemakerlogger at import (insagemaker/core/__init__.py, the universal dependency), so SDK log records aresafely discarded until the application configures logging.
get_sagemaker_config_logger()now only sets a defaultINFOlevel when unsetand lets records propagate to the application's handlers, rather than
attaching a stdout handler or disabling propagation. Removed the now-unused
import sys.Behavior note: config-substitution INFO messages are still emitted on the
sagemaker.configlogger; applications that configure logging (e.g.logging.basicConfig(level=logging.INFO)) receive them through their ownhandlers/formatting instead of an SDK-owned stdout handler.
Testing:
tests/unit/config/test_config_logging.py: asserts no stdout StreamHandleris attached, propagation is not disabled, the INFO default is applied, and the
root
sagemakerlogger has aNullHandlerafter import.tests/unit/configshows no new failures from this change (the pre-existingenvironment-specific failures reproduce identically on master).
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.