Skip to content

Commit edcbb99

Browse files
Passing the method implementation to the formatter class
1 parent 33fb1a6 commit edcbb99

File tree

2 files changed

+51
-26
lines changed

2 files changed

+51
-26
lines changed

aws_lambda_powertools/logging/formatter.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
import time
88
import traceback
99
from abc import ABCMeta, abstractmethod
10+
from contextlib import contextmanager
1011
from contextvars import ContextVar
1112
from datetime import datetime, timezone
1213
from functools import partial
13-
from typing import TYPE_CHECKING, Any, Callable, Iterable
14+
from typing import TYPE_CHECKING, Any, Callable, Generator, Iterable
1415

1516
from aws_lambda_powertools.shared import constants
1617
from aws_lambda_powertools.shared.functions import powertools_dev_is_set
@@ -62,6 +63,10 @@ def clear_state(self) -> None:
6263
"""Removes any previously added logging keys"""
6364
raise NotImplementedError()
6465

66+
@contextmanager
67+
def append_context_keys(self, **additional_keys: Any) -> Generator[None, None, None]:
68+
yield
69+
6570
# These specific thread-safe methods are necessary to manage shared context in concurrent environments.
6671
# They prevent race conditions and ensure data consistency across multiple threads.
6772
def thread_safe_append_keys(self, **additional_keys) -> None:
@@ -263,6 +268,31 @@ def clear_state(self) -> None:
263268
self.log_format = dict.fromkeys(self.log_record_order)
264269
self.log_format.update(**self.keys_combined)
265270

271+
@contextmanager
272+
def append_context_keys(self, **additional_keys: Any) -> Generator[None, None, None]:
273+
"""
274+
Context manager to temporarily add logging keys.
275+
276+
Parameters:
277+
-----------
278+
**keys: Any
279+
Key-value pairs to include in the log context during the lifespan of the context manager.
280+
281+
Example:
282+
--------
283+
>>> logger = Logger(service="example_service")
284+
>>> with logger.append_context_keys(user_id="123", operation="process"):
285+
>>> logger.info("Log with context")
286+
>>> logger.info("Log without context")
287+
"""
288+
# Add keys to the context
289+
self.append_keys(**additional_keys)
290+
try:
291+
yield
292+
finally:
293+
# Remove the keys after exiting the context
294+
self.remove_keys(additional_keys.keys())
295+
266296
# These specific thread-safe methods are necessary to manage shared context in concurrent environments.
267297
# They prevent race conditions and ensure data consistency across multiple threads.
268298
def thread_safe_append_keys(self, **additional_keys) -> None:

aws_lambda_powertools/logging/logger.py

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -330,31 +330,6 @@ def _configure_sampling(self) -> None:
330330
),
331331
)
332332

333-
@contextmanager
334-
def append_context_keys(self, **keys: Any) -> Generator[Any, Any, Any]:
335-
"""
336-
Context manager to temporarily add logging keys.
337-
338-
Parameters:
339-
-----------
340-
**keys: Any
341-
Key-value pairs to include in the log context during the lifespan of the context manager.
342-
343-
Example:
344-
--------
345-
>>> logger = Logger(service="example_service")
346-
>>> with logger.append_context_keys(user_id="123", operation="process"):
347-
>>> logger.info("Log with context")
348-
>>> logger.info("Log without context")
349-
"""
350-
# Add keys to the context
351-
self.append_keys(**keys)
352-
try:
353-
yield
354-
finally:
355-
# Remove the keys after exiting the context
356-
self.remove_keys(keys.keys())
357-
358333
@overload
359334
def inject_lambda_context(
360335
self,
@@ -606,6 +581,26 @@ def get_current_keys(self) -> dict[str, Any]:
606581
def remove_keys(self, keys: Iterable[str]) -> None:
607582
self.registered_formatter.remove_keys(keys)
608583

584+
@contextmanager
585+
def append_context_keys(self, **additional_keys: Any) -> Generator[None, None, None]:
586+
"""
587+
Context manager to temporarily add logging keys.
588+
589+
Parameters:
590+
-----------
591+
**keys: Any
592+
Key-value pairs to include in the log context during the lifespan of the context manager.
593+
594+
Example:
595+
--------
596+
>>> logger = Logger(service="example_service")
597+
>>> with logger.append_context_keys(user_id="123", operation="process"):
598+
>>> logger.info("Log with context")
599+
>>> logger.info("Log without context")
600+
"""
601+
with self.registered_formatter.append_context_keys(**additional_keys):
602+
yield
603+
609604
# These specific thread-safe methods are necessary to manage shared context in concurrent environments.
610605
# They prevent race conditions and ensure data consistency across multiple threads.
611606
def thread_safe_append_keys(self, **additional_keys: object) -> None:

0 commit comments

Comments
 (0)