Skip to content

Commit 7a75988

Browse files
committed
docs(logger): middleware
1 parent 8b6e2bb commit 7a75988

File tree

3 files changed

+95
-28
lines changed

3 files changed

+95
-28
lines changed

docs/core/logger.md

+31-13
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"
77+
```typescript hl_lines="2 10-11"
7878
import { Logger } from "@aws-lambda-powertools/logger";
79+
import { injectLambdaContext } from '@aws-lambda-powertools/logger/middleware/middy';
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
},

packages/logger/README.md

+59-11
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ npm run test
1010
npm run example:hello-world
1111
npm run example:inject-context
1212
npm run example:inject-context-decorator
13+
npm run example:inject-context-middleware
1314
npm run example:errors
1415
npm run example:constructor-options
1516
npm run example:custom-log-formatter
@@ -70,19 +71,19 @@ logger.error('This is an ERROR log');
7071

7172
### Capturing Lambda context info
7273

73-
Without decorators:
74+
With the middy middleware `injectLambdaContext`:
7475

7576
```typescript
7677
// Environment variables set for the Lambda
7778
process.env.LOG_LEVEL = 'INFO';
7879
process.env.POWERTOOLS_SERVICE_NAME = 'hello-world';
80+
import { injectLambdaContext } from '../src/middleware/middy';
81+
import middy from '@middy/core';
7982

8083
const logger = new Logger();
8184

8285
const lambdaHandler: Handler = async () => {
8386

84-
logger.addContext(context);
85-
8687
logger.info('This is an INFO log');
8788

8889
return {
@@ -91,6 +92,9 @@ const lambdaHandler: Handler = async () => {
9192

9293
};
9394

95+
const handlerWithMiddleware = middy(lambdaHandler)
96+
.use(injectLambdaContext(logger));
97+
9498
```
9599

96100
<details>
@@ -99,7 +103,7 @@ const lambdaHandler: Handler = async () => {
99103
```bash
100104

101105
{
102-
aws_request_id: 'c6af9ac6-7b61-11e6-9a41-93e8deadbeef',
106+
aws_request_id: 'c6af9ac6-7b61-11e6-9a41-93e812345678',
103107
cold_start: true,
104108
lambda_function_arn: 'arn:aws:lambda:eu-central-1:123456789012:function:Example',
105109
lambda_function_memory_size: 128,
@@ -111,7 +115,7 @@ const lambdaHandler: Handler = async () => {
111115
xray_trace_id: 'abcdef123456abcdef123456abcdef123456'
112116
}
113117
{
114-
aws_request_id: 'c6af9ac6-7b61-11e6-9a41-93e8deadbeef',
118+
aws_request_id: 'c6af9ac6-7b61-11e6-9a41-93e812345678',
115119
cold_start: true,
116120
lambda_function_arn: 'arn:aws:lambda:eu-central-1:123456789012:function:Example',
117121
lambda_function_memory_size: 128,
@@ -126,6 +130,50 @@ const lambdaHandler: Handler = async () => {
126130
```
127131
</details>
128132

133+
With the `addContext` method:
134+
135+
```typescript
136+
// Environment variables set for the Lambda
137+
process.env.LOG_LEVEL = 'INFO';
138+
process.env.POWERTOOLS_SERVICE_NAME = 'hello-world';
139+
140+
const logger = new Logger();
141+
142+
const lambdaHandler: Handler = async () => {
143+
144+
logger.addContext(context);
145+
146+
logger.info('This is an INFO log');
147+
148+
return {
149+
foo: 'bar'
150+
};
151+
152+
};
153+
154+
```
155+
156+
<details>
157+
<summary>Click to expand and see the logs outputs</summary>
158+
159+
```bash
160+
161+
{
162+
cold_start: true,
163+
function_arn: 'arn:aws:lambda:eu-central-1:123456789012:function:foo-bar-function',
164+
function_memory_size: 128,
165+
function_request_id: 'c6af9ac6-7b61-11e6-9a41-93e812345678',
166+
function_name: 'foo-bar-function',
167+
level: 'INFO',
168+
message: 'This is an INFO log',
169+
service: 'hello-world',
170+
timestamp: '2021-12-15T23:56:17.773Z',
171+
xray_trace_id: 'abcdef123456abcdef123456abcdef123456'
172+
}
173+
174+
```
175+
</details>
176+
129177

130178
With decorators:
131179

@@ -157,15 +205,15 @@ new Lambda().handler(dummyEvent, dummyContext, () => console.log('Lambda invoked
157205
```bash
158206

159207
{
160-
aws_request_id: 'c6af9ac6-7b61-11e6-9a41-93e8deadbeef',
161208
cold_start: true,
162-
lambda_function_arn: 'arn:aws:lambda:eu-central-1:123456789012:function:Example',
163-
lambda_function_memory_size: 128,
164-
lambda_function_name: 'foo-bar-function',
209+
function_arn: 'arn:aws:lambda:eu-central-1:123456789012:function:foo-bar-function',
210+
function_memory_size: 128,
211+
function_request_id: 'c6af9ac6-7b61-11e6-9a41-93e812345678',
212+
function_name: 'foo-bar-function',
165213
level: 'INFO',
166214
message: 'This is an INFO log with some context',
167215
service: 'hello-world',
168-
timestamp: '2021-03-25T11:00:01.400Z',
216+
timestamp: '2021-12-15T23:57:25.749Z',
169217
xray_trace_id: 'abcdef123456abcdef123456abcdef123456'
170218
}
171219

@@ -536,7 +584,7 @@ const lambdaHandler: Handler = async (event, context) => {
536584
service: 'hello-world',
537585
awsRegion: 'eu-central-1',
538586
correlationIds: {
539-
awsRequestId: 'c6af9ac6-7b61-11e6-9a41-93e8deadbeef',
587+
awsRequestId: 'c6af9ac6-7b61-11e6-9a41-93e812345678',
540588
xRayTraceId: 'abcdef123456abcdef123456abcdef123456',
541589
myCustomCorrelationId: 'foo-bar-baz'
542590
},

packages/logger/examples/inject-context-middleware.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,27 @@ require('./../tests/helpers/populateEnvironmentVariables');
55
process.env.LOG_LEVEL = 'INFO';
66
process.env.POWERTOOLS_SERVICE_NAME = 'hello-world';
77

8+
import * as dummyEvent from '../../../tests/resources/events/custom/hello-world.json';
89
import { Handler } from 'aws-lambda';
910
import { Logger } from '../src';
11+
import { context as dummyContext } from '../../../tests/resources/contexts/hello-world';
1012
import { injectLambdaContext } from '../src/middleware/middy';
1113
import middy from '@middy/core';
1214

1315
const logger = new Logger();
1416

1517
const lambdaHandler: Handler = async () => {
1618

17-
logger.debug('This is a DEBUG log');
1819
logger.info('This is an INFO log');
19-
logger.warn('This is a WARN log');
20-
logger.error('This is an ERROR log');
2120

2221
return {
2322
foo: 'bar'
2423
};
2524

2625
};
2726

28-
const lambdaHandlerWithMiddleware = middy(lambdaHandler)
27+
const handlerWithMiddleware = middy(lambdaHandler)
2928
.use(injectLambdaContext(logger));
3029

30+
handlerWithMiddleware(dummyEvent, dummyContext, () => console.log('Lambda invoked!'));
31+

0 commit comments

Comments
 (0)