Skip to content

Commit d25d008

Browse files
committed
fix(logger): caplog working with parent Logger
1 parent 3d2a142 commit d25d008

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

aws_lambda_powertools/logging/logger.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,8 @@ def _init_logger(
374374
if not self._is_deduplication_disabled:
375375
logger.debug("Adding filter in root logger to suppress child logger records to bubble up")
376376
for handler in logging.root.handlers:
377+
if type(handler).__name__ == "LogCaptureHandler" and type(handler).__module__ == "_pytest.logging":
378+
continue
377379
# It'll add a filter to suppress any child logger from self.service
378380
# Example: `Logger(service="order")`, where service is Order
379381
# It'll reject all loggers starting with `order` e.g. order.checkout, order.shared

tests/functional/logger/required_dependencies/test_logger.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from typing import TYPE_CHECKING, Any
1616

1717
import pytest
18+
from _pytest.logging import LogCaptureHandler
1819

1920
from aws_lambda_powertools import Logger
2021
from aws_lambda_powertools.logging import correlation_paths
@@ -1556,3 +1557,24 @@ def handler(event, context):
15561557
# THEN we must be able to inject context
15571558
log = capture_logging_output(stdout)
15581559
assert request_id == log["correlation_id"]
1560+
1561+
1562+
def test_non_preconfigured_logger_with_caplog(caplog, service_name):
1563+
caplog.set_level("INFO")
1564+
logger = Logger(service=service_name)
1565+
logger.info("testing, testing...")
1566+
pytest_handler_existence = any(isinstance(item, LogCaptureHandler) for item in logger._logger.root.handlers)
1567+
1568+
assert pytest_handler_existence is True
1569+
assert len(caplog.records) == 1
1570+
assert caplog.records[0].message == "testing, testing..."
1571+
1572+
1573+
def test_child_logger_with_caplog(caplog):
1574+
caplog.set_level("INFO")
1575+
logger = Logger(child=True)
1576+
logger.info("testing, testing...")
1577+
pytest_handler_existence = any(isinstance(item, LogCaptureHandler) for item in logger._logger.root.handlers)
1578+
1579+
assert len(caplog.records) == 1
1580+
assert pytest_handler_existence is True

0 commit comments

Comments
 (0)