Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(logger): implement addFilter/removeFilter to address static typing errors #3380

Merged
merged 7 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion aws_lambda_powertools/logging/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ def exception(
self,
msg: object,
*args,
exc_info=True,
exc_info: logging._ExcInfoType = True,
stack_info: bool = False,
stacklevel: int = 2,
extra: Optional[Mapping[str, object]] = None,
Expand Down Expand Up @@ -683,6 +683,12 @@ def setLevel(self, level: Union[str, int, None]) -> None:
def addHandler(self, handler: logging.Handler) -> None:
return self._logger.addHandler(handler)

def addFilter(self, filter: logging._FilterType) -> None: # noqa: A002 # filter built-in usage
return self._logger.addFilter(filter)

def removeFilter(self, filter: logging._FilterType) -> None: # noqa: A002 # filter built-in usage
return self._logger.removeFilter(filter)

@property
def registered_handler(self) -> logging.Handler:
"""Convenience property to access the first logger handler"""
Expand Down
26 changes: 26 additions & 0 deletions tests/functional/test_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import logging
import random
import re
import secrets
import string
import sys
import warnings
Expand Down Expand Up @@ -1076,3 +1077,28 @@ def test_log_level_advanced_logging_controler_warning_different_log_levels_using

# THEN Logger must be INFO because it takes precedence over POWERTOOLS_LOG_LEVEL
assert logger.log_level == logging.INFO


def test_logger_add_remove_filter(stdout, service_name):
# GIVEN a Logger with a custom logging filter
class ApiKeyFilter(logging.Filter): # NOSONAR # need filter to test actual impl.
def filter(self, record):
if getattr(record, "api_key", None):
record.api_key = "REDACTED"

return True

redact_api_key_filter = ApiKeyFilter()
logger = Logger(service=service_name, stream=stdout)
logger.addFilter(redact_api_key_filter)

# WHEN a new log statement is issued
# AND another log statement is issued after filter is removed
logger.info("filtered", api_key=secrets.token_urlsafe())
logger.removeFilter(redact_api_key_filter)
logger.info("unfiltered", api_key=secrets.token_urlsafe())

# THEN logging filter should be called and mutate the log record accordingly
log = capture_multiple_logging_statements_output(stdout)
assert log[0]["api_key"] == "REDACTED"
assert log[1]["api_key"] != "REDACTED"