-
Notifications
You must be signed in to change notification settings - Fork 406
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(logger): add context manager for logger keys (#5883)
* add context manager to logger * Passing the method implementation to the formatter class * modify logger tests * add examples to doc --------- Co-authored-by: Leandro Damascena <lcdama@amazon.pt>
- Loading branch information
1 parent
d82537c
commit e96608e
Showing
6 changed files
with
206 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[ | ||
{ | ||
"level": "INFO", | ||
"location": "lambda_handler:8", | ||
"message": "Log with context", | ||
"timestamp": "2024-03-21T10:30:00.123Z", | ||
"service": "example_service", | ||
"user_id": "123", | ||
"operation": "process" | ||
}, | ||
{ | ||
"level": "INFO", | ||
"location": "lambda_handler:10", | ||
"message": "Log without context", | ||
"timestamp": "2024-03-21T10:30:00.124Z", | ||
"service": "example_service" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from aws_lambda_powertools import Logger | ||
from aws_lambda_powertools.utilities.typing import LambdaContext | ||
|
||
logger = Logger(service="example_service") | ||
|
||
|
||
def lambda_handler(event: dict, context: LambdaContext) -> str: | ||
with logger.append_context_keys(user_id="123", operation="process"): | ||
logger.info("Log with context") | ||
|
||
logger.info("Log without context") | ||
|
||
return "hello world" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters