Skip to content

Commit fc5e8a6

Browse files
committed
refactor: make logger e2e dynamic
1 parent 3cce3a0 commit fc5e8a6

File tree

3 files changed

+13
-20
lines changed

3 files changed

+13
-20
lines changed
+4-10
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
1-
import os
2-
31
from aws_lambda_powertools import Logger
42

53
logger = Logger()
64

7-
MESSAGE = os.environ["MESSAGE"]
8-
ADDITIONAL_KEY = os.environ["ADDITIONAL_KEY"]
9-
105

11-
@logger.inject_lambda_context(log_event=True)
6+
@logger.inject_lambda_context
127
def lambda_handler(event, context):
13-
logger.debug(MESSAGE)
14-
logger.info(MESSAGE)
15-
logger.append_keys(**{ADDITIONAL_KEY: "test"})
16-
logger.info(MESSAGE)
8+
message, append_keys = event.get("message", ""), event.get("append_keys", {})
9+
logger.append_keys(**append_keys)
10+
logger.info(message)
1711
return "success"

tests/e2e/logger/infrastructure.py

+1-9
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,8 @@
44

55

66
class LoggerStack(BaseInfrastructureV2):
7-
LOG_MESSAGE: str = "logger message test"
8-
LOG_LEVEL: str = "INFO"
9-
107
def __init__(self, handlers_dir: Path, feature_name: str = "logger") -> None:
118
super().__init__(feature_name, handlers_dir)
129

1310
def create_resources(self):
14-
env_vars = {
15-
"MESSAGE": self.LOG_MESSAGE,
16-
"LOG_LEVEL": self.LOG_LEVEL,
17-
"ADDITIONAL_KEY": "extra_info",
18-
}
19-
self.create_lambda_functions(function_props={"environment": env_vars})
11+
self.create_lambda_functions()

tests/e2e/logger/test_logger.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import json
2+
from uuid import uuid4
3+
14
import pytest
25

36
from tests.e2e.utils import data_fetcher
@@ -23,10 +26,14 @@ def test_basic_lambda_logs_visible(basic_handler_fn, basic_handler_fn_arn):
2326
"function_name",
2427
"cold_start",
2528
)
29+
message = "logs should be visible with default settings"
30+
additional_keys = {"order_id": f"{uuid4()}"}
31+
payload = json.dumps({"message": message, "append_keys": additional_keys})
2632

2733
# WHEN
28-
_, execution_time = data_fetcher.get_lambda_response(lambda_arn=basic_handler_fn_arn)
34+
_, execution_time = data_fetcher.get_lambda_response(lambda_arn=basic_handler_fn_arn, payload=payload)
2935
filtered_logs = data_fetcher.get_logs(function_name=basic_handler_fn, start_time=execution_time)
3036

3137
# THEN
3238
assert all(keys in logs.dict(exclude_unset=True) for logs in filtered_logs for keys in required_keys)
39+
assert any(getattr(logs, "order_id", False) for logs in filtered_logs)

0 commit comments

Comments
 (0)