Skip to content

Commit f5bfb89

Browse files
committed
refactor - readability and pytests support
Signed-off-by: Gal Hubara Agam <96368689+galagam@users.noreply.github.com>
1 parent 7449e2a commit f5bfb89

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

modelopt/onnx/autocast/logging_config.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,39 @@
2929
logger = logging.getLogger("modelopt.onnx.autocast")
3030

3131

32-
def configure_logging(level=logging.INFO, log_file=None):
32+
def configure_logging(level=None, log_file=None):
3333
"""Configure logging for all AutoCast components.
3434
35+
If logging level is provided, it will be used regardless of parent logger log level.
36+
Otherwise, inherits from parent logger if exists, or fallback to default: logging.INFO.
37+
3538
Args:
3639
level: The logging level to use. Can be a string (e.g., "DEBUG", "INFO") or
37-
a logging constant (e.g., logging.DEBUG). Default: logging.INFO.
40+
a logging constant (e.g., logging.DEBUG) default: None.
3841
log_file: Optional path to a log file. If provided, logs will be written to this file
3942
in addition to stdout (default: None).
4043
"""
4144
# Check if parent logger (modelopt.onnx) already has handlers configured
4245
parent_logger = logging.getLogger("modelopt.onnx")
4346
parent_has_handlers = len(parent_logger.handlers) > 0
4447

45-
# If parent is configured and no explicit level provided, inherit parent's level
46-
if parent_has_handlers and level == logging.INFO:
47-
level = parent_logger.level
48+
# Determine the logging level to use
49+
if level is None:
50+
# No explicit level provided - inherit from parent or use default
51+
if parent_has_handlers:
52+
level = parent_logger.level
53+
else:
54+
level = logging.INFO
55+
# else: use the provided level as-is
4856

4957
# Set level for the autocast logger (accepts both string and int)
5058
logger.setLevel(level)
5159

60+
# If parent has handlers (standalone mode), also update parent's level
61+
# so the parent's console handler respects the autocast log level
62+
if parent_has_handlers:
63+
parent_logger.setLevel(level)
64+
5265
# Remove any existing handlers to ensure clean configuration
5366
for handler in logger.handlers[:]:
5467
logger.removeHandler(handler)
@@ -82,7 +95,8 @@ def configure_logging(level=logging.INFO, log_file=None):
8295
console_handler = logging.StreamHandler(sys.stdout)
8396
console_handler.setFormatter(formatter)
8497
logger.addHandler(console_handler)
85-
logger.propagate = False
98+
# Always propagate to support pytest's caplog fixture in tests
99+
logger.propagate = True
86100

87101
# Ensure all child loggers inherit the level setting
88102
for name in logging.root.manager.loggerDict:

modelopt/onnx/logging_config.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,10 @@ def configure_logging(level=logging.INFO, log_file=None):
6464
console_handler.setFormatter(formatter)
6565
logger.addHandler(console_handler)
6666

67-
# Prevent log messages from propagating to the root logger
68-
logger.propagate = False
67+
# Allow log messages to propagate to the root logger for testing compatibility
68+
# This enables pytest's caplog fixture to capture logs while still maintaining
69+
# our custom formatting through the handlers above
70+
logger.propagate = True
6971

7072
# Ensure all child loggers inherit the level setting
7173
for name in logging.root.manager.loggerDict:

0 commit comments

Comments
 (0)