|
9 | 9 | import string
|
10 | 10 | import sys
|
11 | 11 | import warnings
|
12 |
| -from ast import Dict |
13 | 12 | from collections import namedtuple
|
14 | 13 | from datetime import datetime, timezone
|
15 |
| -from typing import Any, Callable, Iterable, List, Optional, Union |
| 14 | +from typing import Any, Callable, Dict, Iterable, List, Optional, Union |
16 | 15 |
|
17 | 16 | import pytest
|
18 | 17 |
|
@@ -606,6 +605,45 @@ def test_logger_append_remove_keys(stdout, service_name):
|
606 | 605 | assert (extra_keys.items() <= keys_removed_log.items()) is False
|
607 | 606 |
|
608 | 607 |
|
| 608 | +def test_logger_append_and_show_current_keys(stdout, service_name): |
| 609 | + # GIVEN a Logger is initialized |
| 610 | + logger = Logger(service=service_name, stream=stdout) |
| 611 | + extra_keys = {"request_id": "id", "context": "value"} |
| 612 | + |
| 613 | + # WHEN keys are updated |
| 614 | + logger.append_keys(**extra_keys) |
| 615 | + |
| 616 | + # THEN appended keys must be present in logger |
| 617 | + current_keys = logger.get_current_keys() |
| 618 | + assert "request_id" in current_keys |
| 619 | + assert "context" in current_keys |
| 620 | + |
| 621 | + |
| 622 | +def test_logger_formatter_without_get_current_keys_method(stdout, service_name): |
| 623 | + class CustomFormatter(BasePowertoolsFormatter): |
| 624 | + def append_keys(self, **additional_keys): |
| 625 | + # Fake method |
| 626 | + pass |
| 627 | + |
| 628 | + def clear_state(self) -> None: |
| 629 | + # Fake method |
| 630 | + pass |
| 631 | + |
| 632 | + custom_formater = CustomFormatter() |
| 633 | + |
| 634 | + # GIVEN a Logger is initialized with a Logger Formatter from scratch |
| 635 | + |
| 636 | + logger = Logger(service=service_name, stream=stdout, logger_formatter=custom_formater) |
| 637 | + extra_keys = {"request_id": "id", "context": "value"} |
| 638 | + |
| 639 | + # WHEN keys are updated |
| 640 | + logger.append_keys(**extra_keys) |
| 641 | + |
| 642 | + # THEN the appended keys will not persist |
| 643 | + # unless the customer implements the required methods and persists the log_format |
| 644 | + assert logger.get_current_keys() == {} |
| 645 | + |
| 646 | + |
609 | 647 | def test_logger_custom_formatter(stdout, service_name, lambda_context):
|
610 | 648 | class CustomFormatter(BasePowertoolsFormatter):
|
611 | 649 | custom_format = {}
|
|
0 commit comments