|
5 | 5 | import random
|
6 | 6 | import re
|
7 | 7 | import string
|
| 8 | +import warnings |
8 | 9 | from ast import Dict
|
9 | 10 | from collections import namedtuple
|
10 | 11 | from datetime import datetime, timezone
|
11 | 12 | from typing import Any, Callable, Iterable, List, Optional, Union
|
12 | 13 |
|
13 | 14 | import pytest
|
14 | 15 |
|
15 |
| -from aws_lambda_powertools import Logger, Tracer |
| 16 | +from aws_lambda_powertools import Logger, Tracer, set_package_logger_handler |
16 | 17 | from aws_lambda_powertools.logging import correlation_paths
|
17 | 18 | from aws_lambda_powertools.logging.exceptions import InvalidLoggerSamplingRateError
|
18 | 19 | from aws_lambda_powertools.logging.formatter import (
|
@@ -827,3 +828,33 @@ def handler(event, context, planet, str_end="."):
|
827 | 828 | log = capture_logging_output(stdout)
|
828 | 829 |
|
829 | 830 | assert log["message"] == "Hello World!"
|
| 831 | + |
| 832 | + |
| 833 | +def test_set_package_logger_handler_with_powertools_debug_env_var(stdout, monkeypatch: pytest.MonkeyPatch): |
| 834 | + # GIVEN POWERTOOLS_DEBUG is set |
| 835 | + monkeypatch.setenv(constants.POWERTOOLS_DEBUG_ENV, "1") |
| 836 | + logger = logging.getLogger("aws_lambda_powertools") |
| 837 | + |
| 838 | + # WHEN set_package_logger is used at initialization |
| 839 | + # and any Powertools operation is used (e.g., Tracer) |
| 840 | + set_package_logger_handler(stream=stdout) |
| 841 | + Tracer(disabled=True) |
| 842 | + |
| 843 | + # THEN Tracer debug log statement should be logged |
| 844 | + output = stdout.getvalue() |
| 845 | + assert "Tracing has been disabled" in output |
| 846 | + assert logger.level == logging.DEBUG |
| 847 | + |
| 848 | + |
| 849 | +def test_powertools_debug_env_var_warning(monkeypatch: pytest.MonkeyPatch): |
| 850 | + # GIVEN POWERTOOLS_DEBUG is set |
| 851 | + monkeypatch.setenv(constants.POWERTOOLS_DEBUG_ENV, "1") |
| 852 | + warning_message = "POWERTOOLS_DEBUG environment variable is enabled. Setting logging level to DEBUG." |
| 853 | + |
| 854 | + # WHEN set_package_logger is used at initialization |
| 855 | + # THEN a warning should be emitted |
| 856 | + with warnings.catch_warnings(record=True) as w: |
| 857 | + warnings.simplefilter("default") |
| 858 | + set_package_logger_handler() |
| 859 | + assert len(w) == 1 |
| 860 | + assert str(w[0].message) == warning_message |
0 commit comments