Skip to content

Commit 4bfc1dd

Browse files
author
Roja Reddy Sareddy
committed
fix(core): gate import-time root logger config behind opt-in
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 #5179 Relates-to #5561
1 parent eca6c70 commit 4bfc1dd

2 files changed

Lines changed: 34 additions & 8 deletions

File tree

‎sagemaker-core/src/sagemaker/core/utils/utils.py‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -194,23 +194,28 @@ def get_rich_handler():
194194

195195

196196
def get_textual_rich_logger(name: str, log_level: str = "INFO") -> logging.Logger:
197-
"""Get a logger with textual rich handler.
197+
"""Get a logger, attaching a rich handler only when rich logging is opted in.
198+
199+
Rich logging (a ``RichHandler`` on the root logger via ``logging.basicConfig``,
200+
plus the themed console/traceback) is opt-in, so that importing the SDK does not
201+
reconfigure the root logger or change process-wide log formatting/level. When the
202+
user has not opted in (see :func:`is_rich_logging_enabled`), this returns the named
203+
logger without configuring handlers or levels, leaving logging to the application.
198204
199205
Args:
200206
name (str): The name of the logger
201-
log_level (str): The log level to set.
207+
log_level (str): The log level to set when rich logging is enabled.
202208
Accepted values are: "DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL".
203209
Defaults to the value of "INFO".
204210
205211
Return:
206-
logging.Logger: A textial rich logger.
212+
logging.Logger: The requested logger.
207213
"""
208214
enable_textual_rich_console_and_traceback()
209-
handler = get_rich_handler()
210-
logging.basicConfig(level=getattr(logging, log_level), handlers=[handler])
211-
logger = logging.getLogger(name)
212-
213-
return logger
215+
if is_rich_logging_enabled():
216+
handler = get_rich_handler()
217+
logging.basicConfig(level=getattr(logging, log_level), handlers=[handler])
218+
return logging.getLogger(name)
214219

215220

216221
logger = get_textual_rich_logger(__name__)

‎sagemaker-core/tests/unit/utils/test_rich_logging.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,24 @@ def test_enable_is_idempotent_when_opted_in():
9191
# The one-shot latch prevents re-installing on the second call.
9292
mock_reconfigure.assert_called_once()
9393
mock_install.assert_called_once()
94+
95+
96+
def test_get_logger_does_not_call_basicconfig_when_opted_out():
97+
# Getting a module logger must not reconfigure the root logger by default.
98+
with patch.dict(os.environ, {}, clear=False):
99+
os.environ.pop(RICH_LOGGING_OPT_IN_ENV_VAR, None)
100+
with patch.object(utils.logging, "basicConfig") as mock_basic_config:
101+
returned = utils.get_textual_rich_logger("sagemaker.core.test.optout")
102+
mock_basic_config.assert_not_called()
103+
assert returned is utils.logging.getLogger("sagemaker.core.test.optout")
104+
105+
106+
def test_get_logger_calls_basicconfig_when_opted_in():
107+
with patch.dict(os.environ, {RICH_LOGGING_OPT_IN_ENV_VAR: "1"}):
108+
with (
109+
patch.object(utils, "reconfigure"),
110+
patch.object(utils, "install"),
111+
patch.object(utils.logging, "basicConfig") as mock_basic_config,
112+
):
113+
utils.get_textual_rich_logger("sagemaker.core.test.optin")
114+
mock_basic_config.assert_called_once()

0 commit comments

Comments
 (0)