Skip to content

Commit

Permalink
respect warnings' filters (NVIDIA#10953)
Browse files Browse the repository at this point in the history
* respect warnings' filters

Signed-off-by: Alexandros Koumparoulis <[email protected]>

* Apply isort and black reformatting

Signed-off-by: akoumpa <[email protected]>

---------

Signed-off-by: Alexandros Koumparoulis <[email protected]>
Signed-off-by: akoumpa <[email protected]>
Co-authored-by: akoumpa <[email protected]>
  • Loading branch information
2 people authored and HuiyingLi committed Nov 15, 2024
1 parent 1c161cf commit 7bd6e3e
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions nemo/utils/nemo_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def __init__(self, capture_warnings=True):
self.rank = 0 if is_global_rank_zero() else "UNK"

def _define_logger(self, capture_warnings=True):
""" Creates the logger if not already created. Called in init"""
"""Creates the logger if not already created. Called in init"""

# Use double-checked locking to avoid taking lock unnecessarily.
if self._logger is not None:
Expand Down Expand Up @@ -126,7 +126,7 @@ def record_factory(*args, **kwargs):
self._logger.propagate = False

def remove_stream_handlers(self):
""" Removes StreamHandler that log to stdout and stderr from the logger."""
"""Removes StreamHandler that log to stdout and stderr from the logger."""
if self._logger is None:
raise RuntimeError("Impossible to set handlers if the Logger is not predefined")

Expand Down Expand Up @@ -236,7 +236,7 @@ def set_verbosity(self, verbosity_level):

@contextmanager
def patch_stderr_handler(self, stream):
""" Sends messages that should log to stderr to stream instead. Useful for unittests """
"""Sends messages that should log to stderr to stream instead. Useful for unittests"""
if self._logger is not None:
try:
old_stream = self._handlers["stream_stderr"].stream
Expand Down Expand Up @@ -268,7 +268,7 @@ def patch_stderr_handler(self, stream):

@contextmanager
def patch_stdout_handler(self, stream):
""" Sends messages that should log to stdout to stream instead. Useful for unittests """
"""Sends messages that should log to stdout to stream instead. Useful for unittests"""
if self._logger is not None:
try:
old_stream = self._handlers["stream_stdout"].stream
Expand Down Expand Up @@ -339,13 +339,25 @@ def captureWarnings(self, capture):
warnings.showwarning = self.old_warnings_showwarning
self.old_warnings_showwarning = None

def _warning_is_ignored(self, category):
from warnings import filters

# Search the filters
for action, msg, cat, mod, ln in filters:
# least-common demoninator if multiple filters for the same class.
if cat == category and action == 'ignore':
return True
return False

def _showwarning(self, message, category, filename, lineno, file=None, line=None):
"""
Implementation of showwarnings which redirects to logging.
It will call warnings.formatwarning and will log the resulting string
with level logging.WARNING.
"""
s = warnings.formatwarning(message, category, filename, lineno, line)
if self._warning_is_ignored(category):
return
self.warning("%s", s)

def _logged_once(self, msg, mode):
Expand Down

0 comments on commit 7bd6e3e

Please sign in to comment.