-
Notifications
You must be signed in to change notification settings - Fork 154
feat(logger): middy middleware #313
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
1e1fcbc
feat(logger): middy middleware
saragerion 63eb569
chore(all): remove deadbeef references, improve consistency of values…
saragerion 8b6e2bb
feat(logger): middleware, cold start business logic + tests
saragerion 7a75988
docs(logger): middleware
saragerion b3154be
chore(logger): fix linting
saragerion 0fbbccc
chore(logger): generate npm-shrinkwrap.json
saragerion abfcf43
build: update on-push, on-release to fix eslint error
saragerion 497cc3d
fix: middleware, middy import, examples, on-push, on-pr pipelines
saragerion 5cfdc42
chore: merge conflict
saragerion fcb4a90
fix: post-merge fixes
saragerion db6ad64
chore: linting fix
saragerion d91f6aa
chore: add npm-shrinkwrap.jso
saragerion 212b0d1
chore: update npm-shrinkwrap.json
saragerion 9f7fdcd
fix(logger): lint PowertoolLog
saragerion File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -70,23 +70,22 @@ Key | Example | Note | |
|
||
You can enrich your structured logs with key Lambda context information in multiple ways. | ||
|
||
Method 1, using a class decorator: | ||
Method 1, using a [Middy](https://github.com/middyjs/middy) middleware: | ||
|
||
=== "handler.ts" | ||
|
||
```typescript hl_lines="7" | ||
import { Logger } from "@aws-lambda-powertools/logger"; | ||
```typescript hl_lines="1 9-10" | ||
import { Logger, injectLambdaContext } from "@aws-lambda-powertools/logger"; | ||
import middy from '@middy/core'; | ||
|
||
const logger = new Logger(); | ||
|
||
class Lambda { | ||
|
||
@logger.injectLambdaContext() | ||
public handler() { | ||
logger.info("This is an INFO log with some context"); | ||
} | ||
|
||
} | ||
const lambdaHandler = async () => { | ||
logger.info("This is an INFO log with some context"); | ||
}; | ||
|
||
const handler = middy(lambdaHandler) | ||
.use(injectLambdaContext(logger)); | ||
``` | ||
|
||
Method 2, calling the `addContext` method: | ||
|
@@ -107,6 +106,25 @@ Method 2, calling the `addContext` method: | |
}; | ||
``` | ||
|
||
Method 3, using a class decorator: | ||
|
||
=== "handler.ts" | ||
|
||
```typescript hl_lines="7" | ||
import { Logger } from "@aws-lambda-powertools/logger"; | ||
|
||
const logger = new Logger(); | ||
|
||
class Lambda { | ||
|
||
@logger.injectLambdaContext() | ||
public handler() { | ||
logger.info("This is an INFO log with some context"); | ||
} | ||
|
||
} | ||
``` | ||
|
||
In both case, the printed log will look like this: | ||
|
||
=== "Example CloudWatch Logs excerpt" | ||
|
@@ -116,7 +134,7 @@ In both case, the printed log will look like this: | |
"cold_start": true, | ||
"function_arn": "arn:aws:lambda:eu-central-1:123456789012:function:shopping-cart-api-lambda-prod-eu-central-1", | ||
"function_memory_size": 128, | ||
"function_request_id": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef", | ||
"function_request_id": "c6af9ac6-7b61-11e6-9a41-93e812345678", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Small change I did in this PR: removed all these weird "deadbeef" references |
||
"function_name": "shopping-cart-api-lambda-prod-eu-central-1", | ||
"level": "INFO", | ||
"message": "This is an INFO log with some context", | ||
|
@@ -134,7 +152,7 @@ Key | Example | |
**function_name** `string` | `shopping-cart-api-lambda-prod-eu-central-1` | ||
**function_memory_size**: `int` | `128` | ||
**function_arn**: `string` | `arn:aws:lambda:eu-central-1:123456789012:function:shopping-cart-api-lambda-prod-eu-central-1` | ||
**function_request_id**: `string` | `c6af9ac6-7b61-11e6-9a41-93e8deadbeef` | ||
**function_request_id**: `string` | `c6af9ac6-7b61-11e6-9a41-93e812345678` | ||
|
||
### Appending persistent additional log keys and values | ||
|
||
|
@@ -558,7 +576,7 @@ This is how the printed log would look: | |
"service": "shopping-cart-api", | ||
"awsRegion": "eu-central-1", | ||
"correlationIds": { | ||
"awsRequestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef", | ||
"awsRequestId": "c6af9ac6-7b61-11e6-9a41-93e812345678", | ||
"xRayTraceId": "abcdef123456abcdef123456abcdef123456", | ||
"myCustomCorrelationId": "foo-bar-baz" | ||
}, | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
middy not defined