Skip to content

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 14 commits into from
Dec 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 16 additions & 26 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = {
jest: true,
node: true,
},
extends: ['plugin:@typescript-eslint/eslint-recommended', 'plugin:@typescript-eslint/recommended'],
extends: [ 'plugin:@typescript-eslint/eslint-recommended', 'plugin:@typescript-eslint/recommended' ],
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint'],
settings: {
Expand All @@ -20,11 +20,11 @@ module.exports = {
rules: {
'@typescript-eslint/ban-ts-ignore': ['off'],
'@typescript-eslint/camelcase': ['off'],
'@typescript-eslint/explicit-function-return-type': ['error', { allowExpressions: true }],
'@typescript-eslint/explicit-function-return-type': [ 'error', { allowExpressions: true } ],
'@typescript-eslint/explicit-member-accessibility': 'error',
'@typescript-eslint/indent': ['error', 2, { SwitchCase: 1 }],
'@typescript-eslint/indent': [ 'error', 2, { SwitchCase: 1 } ],
'@typescript-eslint/interface-name-prefix': ['off'],
'@typescript-eslint/member-delimiter-style': ['error', { multiline: { delimiter: 'none' } }],
'@typescript-eslint/member-delimiter-style': [ 'error', { multiline: { delimiter: 'none' } } ],
'@typescript-eslint/member-ordering': [
'error',
{
Expand All @@ -45,33 +45,23 @@ module.exports = {
],
'@typescript-eslint/no-explicit-any': 'error',
'@typescript-eslint/no-inferrable-types': ['off'],
'@typescript-eslint/no-unused-vars': ['error', { argsIgnorePattern: '^_' }],
'@typescript-eslint/no-unused-vars': [ 'error', { argsIgnorePattern: '^_' } ],
'@typescript-eslint/no-use-before-define': ['off'],
'@typescript-eslint/semi': ['error', 'always'],
'array-bracket-spacing': ['error', 'always', { singleValue: false }],
'arrow-body-style': ['error', 'as-needed'],
'computed-property-spacing': ['error', 'never'],
'func-style': ['warn', 'expression'],
indent: ['error', 2, { SwitchCase: 1 }],
'@typescript-eslint/semi': [ 'error', 'always' ],
'array-bracket-spacing': [ 'error', 'always', { singleValue: false } ],
'arrow-body-style': [ 'error', 'as-needed' ],
'computed-property-spacing': [ 'error', 'never' ],
'func-style': [ 'warn', 'expression' ],
indent: [ 'error', 2, { SwitchCase: 1 } ],
'keyword-spacing': 'error',
'newline-before-return': 2,
'no-console': 0,
'no-multi-spaces': ['error', { ignoreEOLComments: false }],
'no-multiple-empty-lines': ['error', { max: 1, maxBOF: 0 }],
'no-multi-spaces': [ 'error', { ignoreEOLComments: false } ],
'no-multiple-empty-lines': [ 'error', { max: 1, maxBOF: 0 } ],
'no-throw-literal': 'error',
'object-curly-spacing': ['error', 'always'],
'object-curly-spacing': [ 'error', 'always' ],
'prefer-arrow-callback': 'error',
quotes: ['error', 'single', { allowTemplateLiterals: true }],
semi: ['error', 'always'],
'sort-imports': [
'error',
{
allowSeparatedGroups: true,
ignoreCase: true,
ignoreDeclarationSort: false,
ignoreMemberSort: true,
memberSyntaxSortOrder: ['all', 'single', 'multiple', 'none'],
},
],
quotes: [ 'error', 'single', { allowTemplateLiterals: true } ],
semi: [ 'error', 'always' ]
},
};
46 changes: 32 additions & 14 deletions docs/core/logger.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

middy not defined

.use(injectLambdaContext(logger));
```

Method 2, calling the `addContext` method:
Expand All @@ -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"
Expand All @@ -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",
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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",
Expand All @@ -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

Expand Down Expand Up @@ -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"
},
Expand Down
Loading