@@ -10,6 +10,7 @@ npm run test
10
10
npm run example:hello-world
11
11
npm run example:inject-context
12
12
npm run example:inject-context-decorator
13
+ npm run example:inject-context-middleware
13
14
npm run example:errors
14
15
npm run example:constructor-options
15
16
npm run example:custom-log-formatter
@@ -70,19 +71,19 @@ logger.error('This is an ERROR log');
70
71
71
72
### Capturing Lambda context info
72
73
73
- Without decorators :
74
+ With the middy middleware ` injectLambdaContext ` :
74
75
75
76
``` typescript
76
77
// Environment variables set for the Lambda
77
78
process .env .LOG_LEVEL = ' INFO' ;
78
79
process .env .POWERTOOLS_SERVICE_NAME = ' hello-world' ;
80
+ import { injectLambdaContext } from ' ../src/middleware/middy' ;
81
+ import middy from ' @middy/core' ;
79
82
80
83
const logger = new Logger ();
81
84
82
85
const lambdaHandler: Handler = async () => {
83
86
84
- logger .addContext (context );
85
-
86
87
logger .info (' This is an INFO log' );
87
88
88
89
return {
@@ -91,6 +92,9 @@ const lambdaHandler: Handler = async () => {
91
92
92
93
};
93
94
95
+ const handlerWithMiddleware = middy (lambdaHandler )
96
+ .use (injectLambdaContext (logger ));
97
+
94
98
```
95
99
96
100
<details >
@@ -99,7 +103,7 @@ const lambdaHandler: Handler = async () => {
99
103
``` bash
100
104
101
105
{
102
- aws_request_id: ' c6af9ac6-7b61-11e6-9a41-93e8deadbeef ' ,
106
+ aws_request_id: ' c6af9ac6-7b61-11e6-9a41-93e812345678 ' ,
103
107
cold_start: true,
104
108
lambda_function_arn: ' arn:aws:lambda:eu-central-1:123456789012:function:Example' ,
105
109
lambda_function_memory_size: 128,
@@ -111,7 +115,7 @@ const lambdaHandler: Handler = async () => {
111
115
xray_trace_id: ' abcdef123456abcdef123456abcdef123456'
112
116
}
113
117
{
114
- aws_request_id: ' c6af9ac6-7b61-11e6-9a41-93e8deadbeef ' ,
118
+ aws_request_id: ' c6af9ac6-7b61-11e6-9a41-93e812345678 ' ,
115
119
cold_start: true,
116
120
lambda_function_arn: ' arn:aws:lambda:eu-central-1:123456789012:function:Example' ,
117
121
lambda_function_memory_size: 128,
@@ -126,6 +130,50 @@ const lambdaHandler: Handler = async () => {
126
130
```
127
131
</details >
128
132
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
+
129
177
130
178
With decorators:
131
179
@@ -157,15 +205,15 @@ new Lambda().handler(dummyEvent, dummyContext, () => console.log('Lambda invoked
157
205
``` bash
158
206
159
207
{
160
- aws_request_id: ' c6af9ac6-7b61-11e6-9a41-93e8deadbeef' ,
161
208
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' ,
165
213
level: ' INFO' ,
166
214
message: ' This is an INFO log with some context' ,
167
215
service: ' hello-world' ,
168
- timestamp: ' 2021-03-25T11:00:01.400Z ' ,
216
+ timestamp: ' 2021-12-15T23:57:25.749Z ' ,
169
217
xray_trace_id: ' abcdef123456abcdef123456abcdef123456'
170
218
}
171
219
@@ -536,7 +584,7 @@ const lambdaHandler: Handler = async (event, context) => {
536
584
service: ' hello-world' ,
537
585
awsRegion: ' eu-central-1' ,
538
586
correlationIds: {
539
- awsRequestId: ' c6af9ac6-7b61-11e6-9a41-93e8deadbeef ' ,
587
+ awsRequestId: ' c6af9ac6-7b61-11e6-9a41-93e812345678 ' ,
540
588
xRayTraceId: ' abcdef123456abcdef123456abcdef123456' ,
541
589
myCustomCorrelationId: ' foo-bar-baz'
542
590
},
0 commit comments