From 692dcd670b67c162d468ad102af3dc3879ab7127 Mon Sep 17 00:00:00 2001 From: Michal Ploski Date: Mon, 14 Mar 2022 16:15:18 +0100 Subject: [PATCH] Add child loggers validation test --- tests/functional/test_logger_utils.py | 30 +++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/functional/test_logger_utils.py b/tests/functional/test_logger_utils.py index cd431d52543..f0efde36589 100644 --- a/tests/functional/test_logger_utils.py +++ b/tests/functional/test_logger_utils.py @@ -6,6 +6,7 @@ from enum import Enum import pytest +from pytest_mock import MockerFixture from aws_lambda_powertools import Logger from aws_lambda_powertools.logging import formatter, utils @@ -195,6 +196,35 @@ def test_copy_config_to_ext_loggers_should_not_break_append_keys(stdout, log_lev powertools_logger.append_keys(key="value") +def test_copy_config_to_ext_loggers_child_loggers_work(stdout): + # GIVEN powertools logger AND child initialized AND + + # GIVEN Loggers are initialized + # create child logger before parent to mimick + # importing logger from another module/file + # as loggers are created in global scope + service = service_name() + child = Logger(stream=stdout, service=service, child=True) + parent = Logger(stream=stdout, service=service) + + # WHEN a child Logger adds an additional key AND parent logger adds additional key + child.structure_logs(append=True, customer_id="value") + parent.structure_logs(append=True, user_id="value") + # WHEN configuration copied from powertools logger + # AND powertools logger and child logger used + utils.copy_config_to_registered_loggers(source_logger=parent) + parent.warning("Logger message") + child.warning("Child logger message") + + # THEN payment_id key added to both powertools logger and child logger + parent_log, child_log = capture_multiple_logging_statements_output(stdout) + assert "customer_id" in parent_log + assert "customer_id" in child_log + assert "user_id" in parent_log + assert "user_id" in child_log + assert child.parent.name == service + + def test_copy_config_to_ext_loggers_no_duplicate_logs(stdout, logger, log_level): # GIVEN an root logger, external logger and powertools logger initialized