fix(core): make rich console and traceback opt-in - #6340
rsareddy0329 wants to merge 3 commits into
Conversation
Importing the SDK ran enable_textual_rich_console_and_traceback() at module import (sagemaker/core/__init__.py, and again via the module-level logger in utils.py), which called rich.traceback.install() -- overriding sys.excepthook -- and reconfigured the process-global rich console. Users who did not want rich tracebacks/console styling had no way to turn it off, and it changed traceback behavior for the whole process just by importing sagemaker. Gate those process-wide side effects behind an opt-in: they now run only when SAGEMAKER_ENABLE_RICH_LOGGING is set (1/true/yes/on) or a caller passes force=True. Default import no longer touches sys.excepthook or the global console. Add is_rich_logging_enabled() and unit tests covering the default no-op, env opt-in, force, and idempotency. Closes aws#5179 Closes aws#5561
|
Claude finished @rsareddy0329's task in 1m 6s —— View job Claude review —
|
Reformat test_rich_logging.py (blank line after docstring, parenthesized context managers) and collapse a one-line return in utils.py to satisfy black --check in the codestyle-doc-tests CI job.
|
Claude finished @rsareddy0329's task in 58s —— View job Claude review —
|
Addresses review feedback: get_textual_rich_logger() still called logging.basicConfig(level=INFO, handlers=[RichHandler(...)]) at import, which runs on a plain "import sagemaker.core" (via the module-level loggers in utils.py, resources.py, and intelligent_defaults_helper.py). That installed a RichHandler on the root logger and forced the root level to INFO for the whole process, defeating the goal of not touching global logging state on import. Gate the basicConfig/RichHandler behind is_rich_logging_enabled() (the same opt-in as the console/traceback). When not opted in, get_textual_rich_logger() just returns logging.getLogger(name) without configuring handlers or levels. Add tests asserting basicConfig is not called on the opt-out path and is called when opted in. Verified in a fresh process: default "import sagemaker.core" leaves the root logger level and handlers unchanged and sys.excepthook as the stdlib default. Relates-to aws#5179 Relates-to aws#5561
|
Claude finished @rsareddy0329's task in 1m 7s —— View job Claude review —
|
|
Superseded by #6341, which now carries these rich-logging opt-in changes byte-identically alongside the related import-time logging fixes (config-logger |
Issue #, if available: Closes #5179, Closes #5561
Description of changes:
Importing the SDK ran
enable_textual_rich_console_and_traceback()at moduleimport time — once in
sagemaker/core/__init__.py, and again as a side effect ofthe module-level logger in
utils.py. That calledrich.traceback.install(),which overrides
sys.excepthookprocess-wide, and reconfigured the globalrichconsole. As reported in #5179 and #5561, simplyimport sagemakerchangedtraceback rendering for the entire process, with no way to turn it off.
This makes those process-wide side effects opt-in:
enable_textual_rich_console_and_traceback()is now a no-op unless the user setsSAGEMAKER_ENABLE_RICH_LOGGINGto1/true/yes/on(case-insensitive), or acaller passes
force=True.default
import sagemakerno longer touchessys.excepthookor the global console.is_rich_logging_enabled()and theRICH_LOGGING_OPT_IN_ENV_VARconstant.Users who want the rich tracebacks/console back set the env var (or call with
force=True); default behavior is now standard Python tracebacks.Testing:
tests/unit/utils/test_rich_logging.py: default no-op, truthy/falsey envparsing, env opt-in,
force=True, and idempotency (16 cases).import sagemaker.corewith no env leavessys.excepthookasthe stdlib default; with
SAGEMAKER_ENABLE_RICH_LOGGING=1it becomesrich.traceback.tests/unit/utils+tests/unit/generated/test_utils.pypass (63).By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.