Skip to content

fix(logger): child logger must respect log level #5950

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

Merged
merged 1 commit into from
Jan 29, 2025
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
6 changes: 5 additions & 1 deletion aws_lambda_powertools/logging/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,11 @@ def _init_logger(
# b) different sampling mechanisms
# c) multiple messages from being logged as handlers can be duplicated
is_logger_preconfigured = getattr(self._logger, LOGGER_ATTRIBUTE_PRECONFIGURED, False)
if self.child or is_logger_preconfigured:
if self.child:
self.setLevel(log_level)
return

if is_logger_preconfigured:
return

self.setLevel(log_level)
Expand Down
15 changes: 15 additions & 0 deletions tests/functional/logger/required_dependencies/test_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -1217,3 +1217,18 @@ def remove_keys(self, keys: Iterable[str]) -> None:
# THEN the context keys should not persist
current_keys = logger.get_current_keys()
assert current_keys == {}


def test_logger_change_level_child_logger(stdout, service_name):
# GIVEN a new Logger and child Logger
logger = Logger(service=service_name, stream=stdout)
child_logger = Logger(service=service_name, child=True, stream=stdout, level="DEBUG")

# WHEN we emit logs for both in DEBUG level
logger.debug("PARENT")
child_logger.debug("CHILD")

# THEN only child log must emit log due to level
logs = list(stdout.getvalue().strip().split("\n"))
assert len(logs) == 1
assert "service" in logs[0]
Loading