diff --git a/aws_lambda_powertools/logging/utils.py b/aws_lambda_powertools/logging/utils.py index 005772c5797..05ac6d5001b 100644 --- a/aws_lambda_powertools/logging/utils.py +++ b/aws_lambda_powertools/logging/utils.py @@ -81,6 +81,8 @@ def _configure_logger(source_logger: Logger, logger: logging.Logger, level: Unio logger.handlers = [] logger.setLevel(level) logger.propagate = False # ensure we don't propagate logs to existing loggers, #1073 + source_logger.append_keys(name="%(name)s") # include logger name, see #1267 + source_logger.debug(f"Logger {logger} reconfigured to use logging level {level}") for source_handler in source_logger.handlers: logger.addHandler(source_handler) diff --git a/docs/core/logger.md b/docs/core/logger.md index 9605017697c..4b16a1eeb71 100644 --- a/docs/core/logger.md +++ b/docs/core/logger.md @@ -608,6 +608,9 @@ for the given name and level to the logging module. By default, this logs all bo You can copy the Logger setup to all or sub-sets of registered external loggers. Use the `copy_config_to_registered_logger` method to do this. +???+ tip + To help differentiate between loggers, we include the standard logger `name` attribute for all loggers we copied configuration to. + By default all registered loggers will be modified. You can change this behavior by providing `include` and `exclude` attributes. You can also provide optional `log_level` attribute external loggers will be configured with. ```python hl_lines="10" title="Cloning Logger config to all other registered standard loggers" diff --git a/tests/functional/test_logger_utils.py b/tests/functional/test_logger_utils.py index 9e9368dfcdc..0e0c7fc7766 100644 --- a/tests/functional/test_logger_utils.py +++ b/tests/functional/test_logger_utils.py @@ -265,3 +265,26 @@ def test_copy_config_to_ext_loggers_no_duplicate_logs(stdout, logger, log_level) logs = capture_multiple_logging_statements_output(stdout) assert {"message": msg} not in logs assert sum(msg in log.values() for log in logs) == 1 + + +def test_logger_name_is_included_during_copy(stdout, logger, log_level): + # GIVEN two external loggers and powertools logger initialized + logger_1: logging.Logger = logger() + logger_2: logging.Logger = logger() + msg = "test message1" + + powertools_logger = Logger(service=service_name(), level=log_level.INFO.value, stream=stdout) + + # WHEN configuration copied from powertools logger to ALL external loggers + # AND external loggers used + utils.copy_config_to_registered_loggers(source_logger=powertools_logger, include={logger_1.name, logger_2.name}) + logger_1.info(msg) + logger_2.info(msg) + powertools_logger.info(msg) + + logger1_log, logger2_log, pt_log = capture_multiple_logging_statements_output(stdout) + + # THEN name attribute should be present in all loggers + assert logger1_log["name"] == logger_1.name + assert logger2_log["name"] == logger_2.name + assert pt_log["name"] == powertools_logger.name