Skip to content

Commit 7e56b8a

Browse files
author
Michal Ploski
committed
Ensure we don't brake bi-directional propagation if child powertools logger is passed as source_logger
1 parent 4fde751 commit 7e56b8a

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

aws_lambda_powertools/logging/utils.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,14 @@ def copy_config_to_registered_loggers(
3636
# 4. Only exclude set? Ignore Logger in the excluding list
3737

3838
# Exclude source and powertools package logger by default
39+
# If source logger is a child ensure we exclude parent logger to not break child logger
40+
# from receiving/pushing updates to keys being added/removed
41+
source_logger_name = source_logger.name.split(".")[0] if source_logger.child else source_logger.name
42+
3943
if exclude:
40-
exclude.update(source_logger.name, PACKAGE_LOGGER)
44+
exclude.update(source_logger_name, PACKAGE_LOGGER)
4145
else:
42-
exclude = {source_logger.name, PACKAGE_LOGGER}
46+
exclude = {source_logger_name, PACKAGE_LOGGER}
4347

4448
# Prepare loggers set
4549
if include:

tests/functional/test_logger_utils.py

+31
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,37 @@ def test_copy_config_to_ext_loggers_should_not_break_append_keys(stdout, log_lev
195195
powertools_logger.append_keys(key="value")
196196

197197

198+
def test_copy_child_config_to_ext_loggers_should_not_break_append_keys(stdout):
199+
# GIVEN powertools logger AND child initialized AND
200+
201+
# GIVEN Loggers are initialized
202+
# create child logger before parent to mimick
203+
# importing logger from another module/file
204+
# as loggers are created in global scope
205+
service = service_name()
206+
child = Logger(stream=stdout, service=service, child=True)
207+
parent = Logger(stream=stdout, service=service)
208+
209+
# WHEN a child Logger adds an additional key AND child logger adds additional key
210+
# AND configuration copied from powertools child logger
211+
# AND powertools logger and child logger used
212+
child.structure_logs(append=True, customer_id="value")
213+
parent.structure_logs(append=True, user_id="value")
214+
utils.copy_config_to_registered_loggers(source_logger=child)
215+
parent.warning("Logger message")
216+
child.warning("Child logger message")
217+
218+
# THEN both custom keys should be propagated bi-directionally in parent and child loggers
219+
# as parent logger won't be touched when config is being copied
220+
parent_log, child_log = capture_multiple_logging_statements_output(stdout)
221+
assert "customer_id" in parent_log
222+
assert "customer_id" in child_log
223+
assert "user_id" in parent_log
224+
assert "user_id" in child_log
225+
assert "Child Logger message" not in parent_log
226+
assert child.parent.name == service
227+
228+
198229
def test_copy_config_to_parent_loggers_only(stdout):
199230
# GIVEN Powertools Logger and Child Logger are initialized
200231
# and Powertools Logger config is copied over

0 commit comments

Comments
 (0)