Skip to content

Commit

Permalink
Allow logs with levels greater than info through filter (#3248)
Browse files Browse the repository at this point in the history
### Types of changes
<!--- Put an `x` in all the boxes that apply, and remove the not
applicable items -->
- [x] Non-breaking change (fix or new feature that would not break
existing functionality).
- [ ] Breaking change (fix or new feature that would cause existing
functionality to change).
- [ ] New tests added to cover the changes.
- [ ] Quick tests passed locally by running `./runtest.sh`.
- [ ] In-line docstrings updated.
- [ ] Documentation updated.
  • Loading branch information
SYangster authored Feb 22, 2025
1 parent 3aefb2d commit 30d32b1
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions nvflare/fuel/utils/log_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ def format(self, record) -> str:
class LoggerNameFilter(logging.Filter):
def __init__(self, logger_names=["nvflare"], exclude_logger_names=[]):
"""Filter log records based on logger names.
Additionally allows all log records with levelno > logging.INFO through.
Args:
logger_names (List[str]): list of logger names to allow through filter
Expand All @@ -253,8 +254,12 @@ def __init__(self, logger_names=["nvflare"], exclude_logger_names=[]):
self.exclude_logger_names = exclude_logger_names

def filter(self, record):
name = record.fullName if hasattr(record, "fullName") else record.name
return not self.matches_name(name, self.exclude_logger_names) and self.matches_name(name, self.logger_names)
name = getattr(record, "fullName", record.name)

is_logger_included = self.matches_name(name, self.logger_names)
is_logger_excluded = self.matches_name(name, self.exclude_logger_names)

return (record.levelno > logging.INFO) or (not is_logger_excluded and is_logger_included)

def matches_name(self, name, logger_names) -> bool:
return any(name.startswith(logger_name) or name.split(".")[-1] == logger_name for logger_name in logger_names)
Expand Down

0 comments on commit 30d32b1

Please sign in to comment.