Skip to content

Commit 1b92a1e

Browse files
authored
feat(logger): middy middleware (#313)
* feat(logger): middy middleware * chore(all): remove deadbeef references, improve consistency of values in test events * feat(logger): middleware, cold start business logic + tests * docs(logger): middleware * chore(logger): fix linting * chore(logger): generate npm-shrinkwrap.json * build: update on-push, on-release to fix eslint error * fix: middleware, middy import, examples, on-push, on-pr pipelines * fix: post-merge fixes * chore: linting fix * chore: add npm-shrinkwrap.jso * chore: update npm-shrinkwrap.json * fix(logger): lint PowertoolLog
1 parent 38c5103 commit 1b92a1e

31 files changed

+14345
-10840
lines changed

.eslintrc.js

+16-26
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module.exports = {
55
jest: true,
66
node: true,
77
},
8-
extends: ['plugin:@typescript-eslint/eslint-recommended', 'plugin:@typescript-eslint/recommended'],
8+
extends: [ 'plugin:@typescript-eslint/eslint-recommended', 'plugin:@typescript-eslint/recommended' ],
99
parser: '@typescript-eslint/parser',
1010
plugins: ['@typescript-eslint'],
1111
settings: {
@@ -20,11 +20,11 @@ module.exports = {
2020
rules: {
2121
'@typescript-eslint/ban-ts-ignore': ['off'],
2222
'@typescript-eslint/camelcase': ['off'],
23-
'@typescript-eslint/explicit-function-return-type': ['error', { allowExpressions: true }],
23+
'@typescript-eslint/explicit-function-return-type': [ 'error', { allowExpressions: true } ],
2424
'@typescript-eslint/explicit-member-accessibility': 'error',
25-
'@typescript-eslint/indent': ['error', 2, { SwitchCase: 1 }],
25+
'@typescript-eslint/indent': [ 'error', 2, { SwitchCase: 1 } ],
2626
'@typescript-eslint/interface-name-prefix': ['off'],
27-
'@typescript-eslint/member-delimiter-style': ['error', { multiline: { delimiter: 'none' } }],
27+
'@typescript-eslint/member-delimiter-style': [ 'error', { multiline: { delimiter: 'none' } } ],
2828
'@typescript-eslint/member-ordering': [
2929
'error',
3030
{
@@ -45,33 +45,23 @@ module.exports = {
4545
],
4646
'@typescript-eslint/no-explicit-any': 'error',
4747
'@typescript-eslint/no-inferrable-types': ['off'],
48-
'@typescript-eslint/no-unused-vars': ['error', { argsIgnorePattern: '^_' }],
48+
'@typescript-eslint/no-unused-vars': [ 'error', { argsIgnorePattern: '^_' } ],
4949
'@typescript-eslint/no-use-before-define': ['off'],
50-
'@typescript-eslint/semi': ['error', 'always'],
51-
'array-bracket-spacing': ['error', 'always', { singleValue: false }],
52-
'arrow-body-style': ['error', 'as-needed'],
53-
'computed-property-spacing': ['error', 'never'],
54-
'func-style': ['warn', 'expression'],
55-
indent: ['error', 2, { SwitchCase: 1 }],
50+
'@typescript-eslint/semi': [ 'error', 'always' ],
51+
'array-bracket-spacing': [ 'error', 'always', { singleValue: false } ],
52+
'arrow-body-style': [ 'error', 'as-needed' ],
53+
'computed-property-spacing': [ 'error', 'never' ],
54+
'func-style': [ 'warn', 'expression' ],
55+
indent: [ 'error', 2, { SwitchCase: 1 } ],
5656
'keyword-spacing': 'error',
5757
'newline-before-return': 2,
5858
'no-console': 0,
59-
'no-multi-spaces': ['error', { ignoreEOLComments: false }],
60-
'no-multiple-empty-lines': ['error', { max: 1, maxBOF: 0 }],
59+
'no-multi-spaces': [ 'error', { ignoreEOLComments: false } ],
60+
'no-multiple-empty-lines': [ 'error', { max: 1, maxBOF: 0 } ],
6161
'no-throw-literal': 'error',
62-
'object-curly-spacing': ['error', 'always'],
62+
'object-curly-spacing': [ 'error', 'always' ],
6363
'prefer-arrow-callback': 'error',
64-
quotes: ['error', 'single', { allowTemplateLiterals: true }],
65-
semi: ['error', 'always'],
66-
'sort-imports': [
67-
'error',
68-
{
69-
allowSeparatedGroups: true,
70-
ignoreCase: true,
71-
ignoreDeclarationSort: false,
72-
ignoreMemberSort: true,
73-
memberSyntaxSortOrder: ['all', 'single', 'multiple', 'none'],
74-
},
75-
],
64+
quotes: [ 'error', 'single', { allowTemplateLiterals: true } ],
65+
semi: [ 'error', 'always' ]
7666
},
7767
};

docs/core/logger.md

+32-14
Original file line numberDiff line numberDiff line change
@@ -70,23 +70,22 @@ Key | Example | Note
7070

7171
You can enrich your structured logs with key Lambda context information in multiple ways.
7272

73-
Method 1, using a class decorator:
73+
Method 1, using a [Middy](https://github.com/middyjs/middy) middleware:
7474

7575
=== "handler.ts"
7676

77-
```typescript hl_lines="7"
78-
import { Logger } from "@aws-lambda-powertools/logger";
77+
```typescript hl_lines="1 9-10"
78+
import { Logger, injectLambdaContext } from "@aws-lambda-powertools/logger";
79+
import middy from '@middy/core';
7980

8081
const logger = new Logger();
81-
82-
class Lambda {
83-
84-
@logger.injectLambdaContext()
85-
public handler() {
86-
logger.info("This is an INFO log with some context");
87-
}
8882

89-
}
83+
const lambdaHandler = async () => {
84+
logger.info("This is an INFO log with some context");
85+
};
86+
87+
const handler = middy(lambdaHandler)
88+
.use(injectLambdaContext(logger));
9089
```
9190

9291
Method 2, calling the `addContext` method:
@@ -107,6 +106,25 @@ Method 2, calling the `addContext` method:
107106
};
108107
```
109108

109+
Method 3, using a class decorator:
110+
111+
=== "handler.ts"
112+
113+
```typescript hl_lines="7"
114+
import { Logger } from "@aws-lambda-powertools/logger";
115+
116+
const logger = new Logger();
117+
118+
class Lambda {
119+
120+
@logger.injectLambdaContext()
121+
public handler() {
122+
logger.info("This is an INFO log with some context");
123+
}
124+
125+
}
126+
```
127+
110128
In both case, the printed log will look like this:
111129

112130
=== "Example CloudWatch Logs excerpt"
@@ -116,7 +134,7 @@ In both case, the printed log will look like this:
116134
"cold_start": true,
117135
"function_arn": "arn:aws:lambda:eu-central-1:123456789012:function:shopping-cart-api-lambda-prod-eu-central-1",
118136
"function_memory_size": 128,
119-
"function_request_id": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef",
137+
"function_request_id": "c6af9ac6-7b61-11e6-9a41-93e812345678",
120138
"function_name": "shopping-cart-api-lambda-prod-eu-central-1",
121139
"level": "INFO",
122140
"message": "This is an INFO log with some context",
@@ -134,7 +152,7 @@ Key | Example
134152
**function_name** `string` | `shopping-cart-api-lambda-prod-eu-central-1`
135153
**function_memory_size**: `int` | `128`
136154
**function_arn**: `string` | `arn:aws:lambda:eu-central-1:123456789012:function:shopping-cart-api-lambda-prod-eu-central-1`
137-
**function_request_id**: `string` | `c6af9ac6-7b61-11e6-9a41-93e8deadbeef`
155+
**function_request_id**: `string` | `c6af9ac6-7b61-11e6-9a41-93e812345678`
138156

139157
### Appending persistent additional log keys and values
140158

@@ -558,7 +576,7 @@ This is how the printed log would look:
558576
"service": "shopping-cart-api",
559577
"awsRegion": "eu-central-1",
560578
"correlationIds": {
561-
"awsRequestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef",
579+
"awsRequestId": "c6af9ac6-7b61-11e6-9a41-93e812345678",
562580
"xRayTraceId": "abcdef123456abcdef123456abcdef123456",
563581
"myCustomCorrelationId": "foo-bar-baz"
564582
},

0 commit comments

Comments
 (0)