|
10 | 10 | import secrets |
11 | 11 | import string |
12 | 12 | import sys |
| 13 | +import warnings |
13 | 14 | from collections import namedtuple |
14 | 15 | from datetime import datetime, timezone |
15 | 16 | from typing import TYPE_CHECKING, Any |
@@ -1558,6 +1559,117 @@ def test_logger_change_level_child_logger(stdout, service_name): |
1558 | 1559 | assert "service" in logs[0] |
1559 | 1560 |
|
1560 | 1561 |
|
| 1562 | +def test_child_logger_without_level_follows_parent_level(stdout, service_name): |
| 1563 | + # GIVEN a child Logger without a level, created before its parent as when imported from another module |
| 1564 | + child_logger = Logger(service=service_name, child=True, stream=stdout) |
| 1565 | + Logger(service=service_name, stream=stdout, level="DEBUG") |
| 1566 | + |
| 1567 | + # WHEN the child emits a DEBUG log |
| 1568 | + child_logger.debug("CHILD") |
| 1569 | + |
| 1570 | + # THEN the child must follow the parent's DEBUG level |
| 1571 | + log = capture_logging_output(stdout) |
| 1572 | + assert log["message"] == "CHILD" |
| 1573 | + assert log["level"] == "DEBUG" |
| 1574 | + assert child_logger.log_level == logging.DEBUG |
| 1575 | + |
| 1576 | + |
| 1577 | +def test_child_logger_without_level_follows_parent_set_level(stdout, service_name): |
| 1578 | + # GIVEN a parent Logger and a child Logger without a level |
| 1579 | + logger = Logger(service=service_name, stream=stdout) |
| 1580 | + child_logger = Logger(service=service_name, child=True, stream=stdout) |
| 1581 | + |
| 1582 | + # WHEN the parent switches to DEBUG after the child was created |
| 1583 | + logger.setLevel("DEBUG") |
| 1584 | + child_logger.debug("CHILD") |
| 1585 | + |
| 1586 | + # THEN the child must follow the new parent level |
| 1587 | + log = capture_logging_output(stdout) |
| 1588 | + assert log["message"] == "CHILD" |
| 1589 | + |
| 1590 | + |
| 1591 | +def test_child_logger_without_level_follows_less_verbose_parent_level(stdout, service_name): |
| 1592 | + # GIVEN a parent Logger with WARNING level and a child Logger without a level |
| 1593 | + Logger(service=service_name, stream=stdout, level="WARNING") |
| 1594 | + child_logger = Logger(service=service_name, child=True, stream=stdout) |
| 1595 | + |
| 1596 | + # WHEN the child emits an INFO log |
| 1597 | + child_logger.info("CHILD") |
| 1598 | + |
| 1599 | + # THEN nothing must be emitted, as the parent level is WARNING |
| 1600 | + assert stdout.getvalue() == "" |
| 1601 | + |
| 1602 | + |
| 1603 | +def test_child_logger_without_level_follows_parent_sampled_debug_level(stdout, service_name): |
| 1604 | + # GIVEN a parent Logger sampling every request at DEBUG level and a child Logger without a level |
| 1605 | + Logger(service=service_name, stream=stdout, sampling_rate=1) |
| 1606 | + child_logger = Logger(service=service_name, child=True, stream=stdout) |
| 1607 | + |
| 1608 | + # WHEN the child emits a DEBUG log |
| 1609 | + child_logger.debug("CHILD") |
| 1610 | + |
| 1611 | + # THEN the child must follow the sampled DEBUG level |
| 1612 | + log = capture_logging_output(stdout) |
| 1613 | + assert log["message"] == "CHILD" |
| 1614 | + |
| 1615 | + |
| 1616 | +def test_child_logger_without_level_follows_parent_level_after_sample_rate_refresh( |
| 1617 | + monkeypatch, |
| 1618 | + lambda_context, |
| 1619 | + stdout, |
| 1620 | + service_name, |
| 1621 | +): |
| 1622 | + # GIVEN sampling is configured for the function but this request isn't sampled |
| 1623 | + monkeypatch.setenv(constants.LOGGER_LOG_SAMPLING_RATE, "0") |
| 1624 | + # GIVEN a parent Logger with DEBUG level and a child Logger without a level decorating the handler |
| 1625 | + Logger(service=service_name, stream=stdout, level="DEBUG") |
| 1626 | + child_logger = Logger(service=service_name, child=True, stream=stdout) |
| 1627 | + |
| 1628 | + @child_logger.inject_lambda_context |
| 1629 | + def handler(event, context): |
| 1630 | + child_logger.debug("CHILD") |
| 1631 | + |
| 1632 | + # WHEN the handler is invoked twice, refreshing the sampling decision on the warm invocation |
| 1633 | + handler({}, lambda_context) |
| 1634 | + handler({}, lambda_context) |
| 1635 | + |
| 1636 | + # THEN the child must keep following the parent's DEBUG level on every invocation |
| 1637 | + logs = capture_multiple_logging_statements_output(stdout) |
| 1638 | + assert [log["message"] for log in logs] == ["CHILD", "CHILD"] |
| 1639 | + |
| 1640 | + |
| 1641 | +def test_child_logger_without_level_follows_parent_level_over_env_var(monkeypatch, stdout, service_name): |
| 1642 | + # GIVEN POWERTOOLS_LOG_LEVEL is DEBUG but the parent Logger has an explicit INFO level |
| 1643 | + monkeypatch.setenv(constants.POWERTOOLS_LOG_LEVEL_ENV, "DEBUG") |
| 1644 | + Logger(service=service_name, stream=stdout, level="INFO") |
| 1645 | + child_logger = Logger(service=service_name, child=True, stream=stdout) |
| 1646 | + |
| 1647 | + # WHEN the child emits a DEBUG log |
| 1648 | + child_logger.debug("CHILD") |
| 1649 | + |
| 1650 | + # THEN nothing must be emitted, as an explicit level takes precedence over POWERTOOLS_LOG_LEVEL |
| 1651 | + assert stdout.getvalue() == "" |
| 1652 | + |
| 1653 | + |
| 1654 | +@pytest.mark.parametrize("child_level", [None, "DEBUG"]) |
| 1655 | +def test_child_logger_respects_advanced_logging_controls(monkeypatch, stdout, service_name, child_level): |
| 1656 | + # GIVEN AWS Lambda Advanced Logging Controls is set to INFO |
| 1657 | + monkeypatch.setenv(constants.LAMBDA_LOG_LEVEL_ENV, "INFO") |
| 1658 | + Logger(service=service_name, stream=stdout) |
| 1659 | + |
| 1660 | + # WHEN a child Logger is created with or without a DEBUG level |
| 1661 | + with warnings.catch_warnings(): |
| 1662 | + warnings.simplefilter("ignore", UserWarning) |
| 1663 | + child_logger = Logger(service=service_name, child=True, stream=stdout, level=child_level) |
| 1664 | + child_logger.debug("CHILD DEBUG") |
| 1665 | + child_logger.info("CHILD INFO") |
| 1666 | + |
| 1667 | + # THEN ALC level must take precedence for the child |
| 1668 | + logs = capture_multiple_logging_statements_output(stdout) |
| 1669 | + assert [log["message"] for log in logs] == ["CHILD INFO"] |
| 1670 | + assert child_logger.log_level == logging.INFO |
| 1671 | + |
| 1672 | + |
1561 | 1673 | def test_clear_state_with_append_keys(): |
1562 | 1674 | # GIVEN a Logger is initialized |
1563 | 1675 | logger = Logger(service="service_name", stream=stdout) |
|
0 commit comments