Skip to content

Commit

Permalink
feat(logger): include logger name attribute when copy_config_to_regis…
Browse files Browse the repository at this point in the history
…tered_logger is used (#1568)
  • Loading branch information
heitorlessa authored Oct 5, 2022
1 parent f04884d commit 18ac4ae
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions aws_lambda_powertools/logging/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions docs/core/logger.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
23 changes: 23 additions & 0 deletions tests/functional/test_logger_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 18ac4ae

Please sign in to comment.