@@ -59,7 +59,7 @@ For a **complete list** of supported environment variables, refer to [this secti
59
59
// You can also pass the parameters in the constructor
60
60
// const logger = new Logger({
61
61
// logLevel: "WARN",
62
- // serviceName: "shopping-cart-api "
62
+ // serviceName: "serverlessAirline "
63
63
// });
64
64
```
65
65
@@ -74,7 +74,7 @@ For a **complete list** of supported environment variables, refer to [this secti
74
74
Environment:
75
75
Variables:
76
76
LOG_LEVEL: WARN
77
- POWERTOOLS_SERVICE_NAME: shopping-cart-api
77
+ POWERTOOLS_SERVICE_NAME: serverlessAirline
78
78
```
79
79
80
80
### Standard structured keys
@@ -105,7 +105,23 @@ Key | Example
105
105
** function_arn** : ` string ` | ` arn:aws:lambda:eu-central-1:123456789012:function:shopping-cart-api-lambda-prod-eu-central-1 `
106
106
** function_request_id** : ` string ` | ` c6af9ac6-7b61-11e6-9a41-93e812345678 `
107
107
108
- === "Middleware"
108
+ === "Manual"
109
+
110
+ ```typescript hl_lines="7"
111
+ import { Logger } from "@aws-lambda-powertools/logger";
112
+
113
+ const logger = new Logger();
114
+
115
+ export const handler = async (_event, context) => {
116
+
117
+ logger.addContext(context);
118
+
119
+ logger.info("This is an INFO log with some context");
120
+
121
+ };
122
+ ```
123
+
124
+ === "Middy Middleware"
109
125
110
126
!!! note
111
127
Middy comes bundled with Logger, so you can just import it when using the middleware.
@@ -119,30 +135,14 @@ Key | Example
119
135
120
136
const logger = new Logger();
121
137
122
- const lambdaHandler = async () => {
138
+ const lambdaHandler = async (_event: any, _context: any ) => {
123
139
logger.info("This is an INFO log with some context");
124
140
};
125
141
126
- const handler = middy(lambdaHandler)
142
+ export const handler = middy(lambdaHandler)
127
143
.use(injectLambdaContext(logger));
128
144
```
129
145
130
- === "Manual"
131
-
132
- ```typescript hl_lines="7"
133
- import { Logger } from "@aws-lambda-powertools/logger";
134
-
135
- const logger = new Logger();
136
-
137
- const lambdaHandler = async (_event, context) => {
138
-
139
- logger.addContext(context);
140
-
141
- logger.info("This is an INFO log with some context");
142
-
143
- };
144
- ```
145
-
146
146
=== "Decorator"
147
147
148
148
```typescript hl_lines="7"
@@ -151,13 +151,16 @@ Key | Example
151
151
const logger = new Logger();
152
152
153
153
class Lambda {
154
-
154
+ // Decorate your handler class method
155
155
@logger.injectLambdaContext()
156
- public handler() {
156
+ public handler(_event: any, _context: any ) {
157
157
logger.info("This is an INFO log with some context");
158
158
}
159
159
160
160
}
161
+
162
+ export const handlerClass = new Lambda();
163
+ export const handler = handlerClass.handler;
161
164
```
162
165
163
166
In each case, the printed log will look like this:
@@ -173,7 +176,7 @@ In each case, the printed log will look like this:
173
176
"function_name": "shopping-cart-api-lambda-prod-eu-central-1",
174
177
"level": "INFO",
175
178
"message": "This is an INFO log with some context",
176
- "service": "shopping-cart-api ",
179
+ "service": "serverlessAirline ",
177
180
"timestamp": "2021-12-12T21:21:08.921Z",
178
181
"xray_trace_id": "abcdef123456abcdef123456abcdef123456"
179
182
}
@@ -213,7 +216,7 @@ You can append additional persistent keys and values in the logs generated durin
213
216
// }
214
217
// });
215
218
216
- const lambdaHandler: Handler = async () => {
219
+ export const handler = async (_event: any, _context: any ) => {
217
220
218
221
// This info log will print all extra custom attributes added above
219
222
// Extra attributes: logger object with name and version of the logger library, awsAccountId, awsRegion
@@ -232,7 +235,7 @@ You can append additional persistent keys and values in the logs generated durin
232
235
{
233
236
"level": "INFO",
234
237
"message": "This is an INFO log",
235
- "service": "shopping-cart-api ",
238
+ "service": "serverlessAirline ",
236
239
"timestamp": "2021-12-12T21:49:58.084Z",
237
240
"xray_trace_id": "abcdef123456abcdef123456abcdef123456",
238
241
"aws_account_id": "123456789012",
@@ -245,7 +248,7 @@ You can append additional persistent keys and values in the logs generated durin
245
248
{
246
249
"level": "INFO",
247
250
"message": "This is another INFO log",
248
- "service": "shopping-cart-api ",
251
+ "service": "serverlessAirline ",
249
252
"timestamp": "2021-12-12T21:49:58.088Z",
250
253
"xray_trace_id": "abcdef123456abcdef123456abcdef123456",
251
254
"aws_account_id": "123456789012",
@@ -271,7 +274,7 @@ You can append additional keys and values in a single log item passing them as p
271
274
272
275
const logger = new Logger();
273
276
274
- const lambdaHandler = async () => {
277
+ export const handler = async (_event: any, _context: any ) => {
275
278
276
279
const myImportantVariable = {
277
280
foo: "bar"
@@ -300,15 +303,15 @@ You can append additional keys and values in a single log item passing them as p
300
303
{
301
304
"level": "INFO",
302
305
"message": "This is a log with an extra variable",
303
- "service": "shopping-cart-api ",
306
+ "service": "serverlessAirline ",
304
307
"timestamp": "2021-12-12T22:06:17.463Z",
305
308
"xray_trace_id": "abcdef123456abcdef123456abcdef123456",
306
309
"data": { foo: "bar" }
307
310
}
308
311
{
309
312
"level": "INFO",
310
313
"message": "This is a log with 2 extra variables",
311
- "service": "shopping-cart-api ",
314
+ "service": "serverlessAirline ",
312
315
"timestamp": "2021-12-12T22:06:17.466Z",
313
316
"xray_trace_id": "abcdef123456abcdef123456abcdef123456",
314
317
"data": { "foo": "bar" },
@@ -328,7 +331,7 @@ The error will be logged with default key name `error`, but you can also pass yo
328
331
329
332
const logger = new Logger();
330
333
331
- const lambdaHandler = async () => {
334
+ export const handler = async (_event: any, _context: any ) => {
332
335
333
336
try {
334
337
throw new Error("Unexpected error #1");
@@ -353,7 +356,7 @@ The error will be logged with default key name `error`, but you can also pass yo
353
356
{
354
357
"level": "ERROR",
355
358
"message": "This is an ERROR log #1",
356
- "service": "shopping-cart-api ",
359
+ "service": "serverlessAirline ",
357
360
"timestamp": "2021-12-12T22:12:39.345Z",
358
361
"xray_trace_id": "abcdef123456abcdef123456abcdef123456",
359
362
"error": {
@@ -366,7 +369,7 @@ The error will be logged with default key name `error`, but you can also pass yo
366
369
{
367
370
"level": "ERROR",
368
371
"message": "This is an ERROR log #2",
369
- "service": "shopping-cart-api ",
372
+ "service": "serverlessAirline ",
370
373
"timestamp": "2021-12-12T22:12:39.377Z",
371
374
"xray_trace_id": "abcdef123456abcdef123456abcdef123456",
372
375
"myCustomErrorKey": {
@@ -400,7 +403,7 @@ This can be useful for example if you want to enable multiple Loggers with diffe
400
403
logLevel: "ERROR"
401
404
});
402
405
403
- const lambdaHandler: Handler = async () => {
406
+ export const handler = async (_event: any, _context: any ) => {
404
407
405
408
logger.info("This is an INFO log, from the parent logger");
406
409
logger.error("This is an ERROR log, from the parent logger");
@@ -417,21 +420,21 @@ This can be useful for example if you want to enable multiple Loggers with diffe
417
420
{
418
421
"level": "INFO",
419
422
"message": "This is an INFO log, from the parent logger",
420
- "service": "shopping-cart-api ",
423
+ "service": "serverlessAirline ",
421
424
"timestamp": "2021-12-12T22:32:54.667Z",
422
425
"xray_trace_id": "abcdef123456abcdef123456abcdef123456"
423
426
}
424
427
{
425
428
"level": "ERROR",
426
429
"message": "This is an ERROR log, from the parent logger",
427
- "service": "shopping-cart-api ",
430
+ "service": "serverlessAirline ",
428
431
"timestamp": "2021-12-12T22:32:54.670Z",
429
432
"xray_trace_id": "abcdef123456abcdef123456abcdef123456"
430
433
}
431
434
{
432
435
"level": "ERROR",
433
436
"message": "This is an ERROR log, from the child logger",
434
- "service": "shopping-cart-api ",
437
+ "service": "serverlessAirline ",
435
438
"timestamp": "2021-12-12T22:32:54.670Z",
436
439
"xray_trace_id": "abcdef123456abcdef123456abcdef123456"
437
440
}
@@ -464,7 +467,7 @@ For example, by setting the "sample rate" to `0.5`, roughly 50% of your lambda i
464
467
sampleRateValue: 0.5
465
468
});
466
469
467
- const lambdaHandler = async () => {
470
+ export const handler = async (_event: any, _context: any ) => {
468
471
469
472
// 0.5 means that you have 50% chance that these logs will be printed
470
473
logger.info("This is INFO log #1");
@@ -485,31 +488,31 @@ For example, by setting the "sample rate" to `0.5`, roughly 50% of your lambda i
485
488
"level": "INFO",
486
489
"message": "This is INFO log #1",
487
490
"sampling_rate": "0.5",
488
- "service": "shopping-cart-api ",
491
+ "service": "serverlessAirline ",
489
492
"timestamp": "2021-12-12T22:59:06.334Z",
490
493
"xray_trace_id": "abcdef123456abcdef123456abcdef123456"
491
494
}
492
495
{
493
496
"level": "INFO",
494
497
"message": "This is INFO log #2",
495
498
"sampling_rate": "0.5",
496
- "service": "shopping-cart-api ",
499
+ "service": "serverlessAirline ",
497
500
"timestamp": "2021-12-12T22:59:06.337Z",
498
501
"xray_trace_id": "abcdef123456abcdef123456abcdef123456"
499
502
}
500
503
{
501
504
"level": "INFO",
502
505
"message": "This is INFO log #3",
503
506
"sampling_rate": "0.5",
504
- "service": "shopping-cart-api ",
507
+ "service": "serverlessAirline ",
505
508
"timestamp": "2021-12-12T22:59:06.338Z",
506
509
"xray_trace_id": "abcdef123456abcdef123456abcdef123456"
507
510
}
508
511
{
509
512
"level": "INFO",
510
513
"message": "This is INFO log #4",
511
514
"sampling_rate": "0.5",
512
- "service": "shopping-cart-api ",
515
+ "service": "serverlessAirline ",
513
516
"timestamp": "2021-12-12T22:59:06.338Z",
514
517
"xray_trace_id": "abcdef123456abcdef123456abcdef123456"
515
518
}
@@ -528,7 +531,7 @@ You can customize the structure (keys and values) of your log items by passing a
528
531
const logger = new Logger({
529
532
logFormatter: new MyCompanyLogFormatter(),
530
533
logLevel: "DEBUG",
531
- serviceName: "shopping-cart-api ",
534
+ serviceName: "serverlessAirline ",
532
535
sampleRateValue: 0.5,
533
536
persistentLogAttributes: {
534
537
awsAccountId: process.env.AWS_ACCOUNT_ID,
@@ -539,11 +542,13 @@ You can customize the structure (keys and values) of your log items by passing a
539
542
},
540
543
});
541
544
542
- const lambdaHandler: Handler = async (event, context) => {
545
+ export const handler = async (event, _context) => {
546
+
543
547
logger.addContext(context);
544
548
545
549
logger.info("This is an INFO log", { correlationIds: { myCustomCorrelationId: "foo-bar-baz" } });
546
- };
550
+
551
+ };
547
552
```
548
553
549
554
This is how the ` MyCompanyLogFormatter ` (dummy name) would look like:
@@ -598,7 +603,7 @@ This is how the printed log would look:
598
603
```json
599
604
{
600
605
"message": "This is an INFO log",
601
- "service": "shopping-cart-api ",
606
+ "service": "serverlessAirline ",
602
607
"awsRegion": "eu-central-1",
603
608
"correlationIds": {
604
609
"awsRequestId": "c6af9ac6-7b61-11e6-9a41-93e812345678",
@@ -654,7 +659,7 @@ describe('MyUnitTest', () => {
654
659
655
660
test (' Lambda invoked successfully' , async () => {
656
661
657
- const testEvent = { test: ' test' };
662
+ const testEvent = { test: ' test' };
658
663
await handler (testEvent , dummyContext );
659
664
660
665
});
0 commit comments