@@ -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
@@ -86,7 +86,7 @@ Key | Example | Note
86
86
** level** : ` string ` | ` INFO ` | Logging level set for the Lambda function"s invocation
87
87
** message** : ` string ` | ` Query performed to DynamoDB ` | A descriptive, human-readable representation of this log item
88
88
** sampling_rate** : ` float ` | ` 0.1 ` | When enabled, it prints all the logs of a percentage of invocations, e.g. 10%
89
- ** service** : ` string ` | ` shopping-cart-api ` | A unique name identifier of the service this Lambda function belongs to, by default ` service_undefined `
89
+ ** service** : ` string ` | ` serverlessAirline ` | A unique name identifier of the service this Lambda function belongs to, by default ` service_undefined `
90
90
** timestamp** : ` string ` | ` 2011-10-05T14:48:00.000Z ` | Timestamp string in simplified extended ISO format (ISO 8601)
91
91
** xray_trace_id** : ` string ` | ` 1-5759e988-bd862e3fe1be46a994272793 ` | When [ tracing is enabled] ( https://docs.aws.amazon.com/lambda/latest/dg/services-xray.html ) {target="_ blank"}, it shows X-Ray Trace ID
92
92
** error** : ` Object ` | ` { name: "Error", location: "/my-project/handler.ts:18", message: "Unexpected error #1", stack: "[stacktrace]"} ` | Optional - An object containing information about the Error passed to the logger
@@ -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,45 +135,33 @@ 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
- ```typescript hl_lines="7 "
148
+ ```typescript hl_lines="8 "
149
149
import { Logger } from "@aws-lambda-powertools/logger";
150
+ import { LambdaInterface } from '@aws-lambda-powertools/commons';
150
151
151
152
const logger = new Logger();
152
153
153
- class Lambda {
154
-
154
+ class Lambda implements LambdaInterface {
155
+ // Decorate your handler class method
155
156
@logger.injectLambdaContext()
156
- public handler() {
157
+ public async handler(_event: any, _context: any): Promise<void> {
157
158
logger.info("This is an INFO log with some context");
158
159
}
159
160
160
161
}
162
+
163
+ export const myFunction = new Lambda();
164
+ export const handler = myFunction.handler;
161
165
```
162
166
163
167
In each case, the printed log will look like this:
@@ -173,7 +177,7 @@ In each case, the printed log will look like this:
173
177
"function_name": "shopping-cart-api-lambda-prod-eu-central-1",
174
178
"level": "INFO",
175
179
"message": "This is an INFO log with some context",
176
- "service": "shopping-cart-api ",
180
+ "service": "serverlessAirline ",
177
181
"timestamp": "2021-12-12T21:21:08.921Z",
178
182
"xray_trace_id": "abcdef123456abcdef123456abcdef123456"
179
183
}
@@ -213,7 +217,7 @@ You can append additional persistent keys and values in the logs generated durin
213
217
// }
214
218
// });
215
219
216
- const lambdaHandler: Handler = async () => {
220
+ export const handler = async (_event: any, _context: any ) => {
217
221
218
222
// This info log will print all extra custom attributes added above
219
223
// Extra attributes: logger object with name and version of the logger library, awsAccountId, awsRegion
@@ -232,7 +236,7 @@ You can append additional persistent keys and values in the logs generated durin
232
236
{
233
237
"level": "INFO",
234
238
"message": "This is an INFO log",
235
- "service": "shopping-cart-api ",
239
+ "service": "serverlessAirline ",
236
240
"timestamp": "2021-12-12T21:49:58.084Z",
237
241
"xray_trace_id": "abcdef123456abcdef123456abcdef123456",
238
242
"aws_account_id": "123456789012",
@@ -245,7 +249,7 @@ You can append additional persistent keys and values in the logs generated durin
245
249
{
246
250
"level": "INFO",
247
251
"message": "This is another INFO log",
248
- "service": "shopping-cart-api ",
252
+ "service": "serverlessAirline ",
249
253
"timestamp": "2021-12-12T21:49:58.088Z",
250
254
"xray_trace_id": "abcdef123456abcdef123456abcdef123456",
251
255
"aws_account_id": "123456789012",
@@ -271,7 +275,7 @@ You can append additional keys and values in a single log item passing them as p
271
275
272
276
const logger = new Logger();
273
277
274
- const lambdaHandler = async () => {
278
+ export const handler = async (_event: any, _context: any ) => {
275
279
276
280
const myImportantVariable = {
277
281
foo: "bar"
@@ -300,15 +304,15 @@ You can append additional keys and values in a single log item passing them as p
300
304
{
301
305
"level": "INFO",
302
306
"message": "This is a log with an extra variable",
303
- "service": "shopping-cart-api ",
307
+ "service": "serverlessAirline ",
304
308
"timestamp": "2021-12-12T22:06:17.463Z",
305
309
"xray_trace_id": "abcdef123456abcdef123456abcdef123456",
306
310
"data": { foo: "bar" }
307
311
}
308
312
{
309
313
"level": "INFO",
310
314
"message": "This is a log with 2 extra variables",
311
- "service": "shopping-cart-api ",
315
+ "service": "serverlessAirline ",
312
316
"timestamp": "2021-12-12T22:06:17.466Z",
313
317
"xray_trace_id": "abcdef123456abcdef123456abcdef123456",
314
318
"data": { "foo": "bar" },
@@ -328,7 +332,7 @@ The error will be logged with default key name `error`, but you can also pass yo
328
332
329
333
const logger = new Logger();
330
334
331
- const lambdaHandler = async () => {
335
+ export const handler = async (_event: any, _context: any ) => {
332
336
333
337
try {
334
338
throw new Error("Unexpected error #1");
@@ -353,7 +357,7 @@ The error will be logged with default key name `error`, but you can also pass yo
353
357
{
354
358
"level": "ERROR",
355
359
"message": "This is an ERROR log #1",
356
- "service": "shopping-cart-api ",
360
+ "service": "serverlessAirline ",
357
361
"timestamp": "2021-12-12T22:12:39.345Z",
358
362
"xray_trace_id": "abcdef123456abcdef123456abcdef123456",
359
363
"error": {
@@ -366,7 +370,7 @@ The error will be logged with default key name `error`, but you can also pass yo
366
370
{
367
371
"level": "ERROR",
368
372
"message": "This is an ERROR log #2",
369
- "service": "shopping-cart-api ",
373
+ "service": "serverlessAirline ",
370
374
"timestamp": "2021-12-12T22:12:39.377Z",
371
375
"xray_trace_id": "abcdef123456abcdef123456abcdef123456",
372
376
"myCustomErrorKey": {
@@ -400,7 +404,7 @@ This can be useful for example if you want to enable multiple Loggers with diffe
400
404
logLevel: "ERROR"
401
405
});
402
406
403
- const lambdaHandler: Handler = async () => {
407
+ export const handler = async (_event: any, _context: any ) => {
404
408
405
409
logger.info("This is an INFO log, from the parent logger");
406
410
logger.error("This is an ERROR log, from the parent logger");
@@ -417,21 +421,21 @@ This can be useful for example if you want to enable multiple Loggers with diffe
417
421
{
418
422
"level": "INFO",
419
423
"message": "This is an INFO log, from the parent logger",
420
- "service": "shopping-cart-api ",
424
+ "service": "serverlessAirline ",
421
425
"timestamp": "2021-12-12T22:32:54.667Z",
422
426
"xray_trace_id": "abcdef123456abcdef123456abcdef123456"
423
427
}
424
428
{
425
429
"level": "ERROR",
426
430
"message": "This is an ERROR log, from the parent logger",
427
- "service": "shopping-cart-api ",
431
+ "service": "serverlessAirline ",
428
432
"timestamp": "2021-12-12T22:32:54.670Z",
429
433
"xray_trace_id": "abcdef123456abcdef123456abcdef123456"
430
434
}
431
435
{
432
436
"level": "ERROR",
433
437
"message": "This is an ERROR log, from the child logger",
434
- "service": "shopping-cart-api ",
438
+ "service": "serverlessAirline ",
435
439
"timestamp": "2021-12-12T22:32:54.670Z",
436
440
"xray_trace_id": "abcdef123456abcdef123456abcdef123456"
437
441
}
@@ -465,7 +469,7 @@ For example, by setting the "sample rate" to `0.5`, roughly 50% of your lambda i
465
469
sampleRateValue: 0.5
466
470
});
467
471
468
- const lambdaHandler = async () => {
472
+ export const handler = async (_event: any, _context: any ) => {
469
473
470
474
// This log item (equal to log level 'ERROR') will be printed to standard output
471
475
// in all Lambda invocations
@@ -490,31 +494,31 @@ For example, by setting the "sample rate" to `0.5`, roughly 50% of your lambda i
490
494
"level": "ERROR",
491
495
"message": "This is an ERROR log",
492
496
"sampling_rate": "0.5",
493
- "service": "shopping-cart-api ",
497
+ "service": "serverlessAirline ",
494
498
"timestamp": "2021-12-12T22:59:06.334Z",
495
499
"xray_trace_id": "abcdef123456abcdef123456abcdef123456"
496
500
}
497
501
{
498
502
"level": "DEBUG",
499
503
"message": "This is a DEBUG log that has 50% chance of being printed",
500
504
"sampling_rate": "0.5",
501
- "service": "shopping-cart-api ",
505
+ "service": "serverlessAirline ",
502
506
"timestamp": "2021-12-12T22:59:06.337Z",
503
507
"xray_trace_id": "abcdef123456abcdef123456abcdef123456"
504
508
}
505
509
{
506
510
"level": "INFO",
507
511
"message": "This is an INFO log that has 50% chance of being printed",
508
512
"sampling_rate": "0.5",
509
- "service": "shopping-cart-api ",
513
+ "service": "serverlessAirline ",
510
514
"timestamp": "2021-12-12T22:59:06.338Z",
511
515
"xray_trace_id": "abcdef123456abcdef123456abcdef123456"
512
516
}
513
517
{
514
518
"level": "WARN",
515
519
"message": "This is a WARN log that has 50% chance of being printed",
516
520
"sampling_rate": "0.5",
517
- "service": "shopping-cart-api ",
521
+ "service": "serverlessAirline ",
518
522
"timestamp": "2021-12-12T22:59:06.338Z",
519
523
"xray_trace_id": "abcdef123456abcdef123456abcdef123456"
520
524
}
@@ -527,7 +531,7 @@ For example, by setting the "sample rate" to `0.5`, roughly 50% of your lambda i
527
531
"level": "ERROR",
528
532
"message": "This is an ERROR log",
529
533
"sampling_rate": "0.5",
530
- "service": "shopping-cart-api ",
534
+ "service": "serverlessAirline ",
531
535
"timestamp": "2021-12-12T22:59:06.334Z",
532
536
"xray_trace_id": "abcdef123456abcdef123456abcdef123456"
533
537
}
@@ -540,31 +544,31 @@ For example, by setting the "sample rate" to `0.5`, roughly 50% of your lambda i
540
544
"level": "ERROR",
541
545
"message": "This is an ERROR log",
542
546
"sampling_rate": "0.5",
543
- "service": "shopping-cart-api ",
547
+ "service": "serverlessAirline ",
544
548
"timestamp": "2021-12-12T22:59:06.334Z",
545
549
"xray_trace_id": "abcdef123456abcdef123456abcdef123456"
546
550
}
547
551
{
548
552
"level": "DEBUG",
549
553
"message": "This is a DEBUG log that has 50% chance of being printed",
550
554
"sampling_rate": "0.5",
551
- "service": "shopping-cart-api ",
555
+ "service": "serverlessAirline ",
552
556
"timestamp": "2021-12-12T22:59:06.337Z",
553
557
"xray_trace_id": "abcdef123456abcdef123456abcdef123456"
554
558
}
555
559
{
556
560
"level": "INFO",
557
561
"message": "This is an INFO log that has 50% chance of being printed",
558
562
"sampling_rate": "0.5",
559
- "service": "shopping-cart-api ",
563
+ "service": "serverlessAirline ",
560
564
"timestamp": "2021-12-12T22:59:06.338Z",
561
565
"xray_trace_id": "abcdef123456abcdef123456abcdef123456"
562
566
}
563
567
{
564
568
"level": "WARN",
565
569
"message": "This is a WARN log that has 50% chance of being printed",
566
570
"sampling_rate": "0.5",
567
- "service": "shopping-cart-api ",
571
+ "service": "serverlessAirline ",
568
572
"timestamp": "2021-12-12T22:59:06.338Z",
569
573
"xray_trace_id": "abcdef123456abcdef123456abcdef123456"
570
574
}
@@ -577,15 +581,15 @@ For example, by setting the "sample rate" to `0.5`, roughly 50% of your lambda i
577
581
"level": "ERROR",
578
582
"message": "This is an ERROR log",
579
583
"sampling_rate": "0.5",
580
- "service": "shopping-cart-api ",
584
+ "service": "serverlessAirline ",
581
585
"timestamp": "2021-12-12T22:59:06.334Z",
582
586
"xray_trace_id": "abcdef123456abcdef123456abcdef123456"
583
587
}
584
588
```
585
589
586
590
### Custom Log formatter (Bring Your Own Formatter)
587
591
588
- You can customize the structure (keys and values) of your log items by passing a custom log formatter, an object that extends the ` LogFormatter ` abstract class.
592
+ You can customize the structure (keys and values) of your log items by passing a custom log formatter, an object that implements the ` LogFormatter ` abstract class.
589
593
590
594
=== "handler.ts"
591
595
@@ -596,7 +600,7 @@ You can customize the structure (keys and values) of your log items by passing a
596
600
const logger = new Logger({
597
601
logFormatter: new MyCompanyLogFormatter(),
598
602
logLevel: "DEBUG",
599
- serviceName: "shopping-cart-api ",
603
+ serviceName: "serverlessAirline ",
600
604
sampleRateValue: 0.5,
601
605
persistentLogAttributes: {
602
606
awsAccountId: process.env.AWS_ACCOUNT_ID,
@@ -607,10 +611,12 @@ You can customize the structure (keys and values) of your log items by passing a
607
611
},
608
612
});
609
613
610
- const lambdaHandler: Handler = async (event, context) => {
614
+ export const handler = async (event, _context) => {
615
+
611
616
logger.addContext(context);
612
617
613
618
logger.info("This is an INFO log", { correlationIds: { myCustomCorrelationId: "foo-bar-baz" } });
619
+
614
620
};
615
621
```
616
622
@@ -625,7 +631,7 @@ This is how the `MyCompanyLogFormatter` (dummy name) would look like:
625
631
// Replace this line with your own type
626
632
type MyCompanyLog = LogAttributes;
627
633
628
- class MyCompanyLogFormatter extends LogFormatter {
634
+ class MyCompanyLogFormatter implements LogFormatter {
629
635
630
636
public formatAttributes(attributes: UnformattedAttributes): MyCompanyLog {
631
637
return {
@@ -666,7 +672,7 @@ This is how the printed log would look:
666
672
```json
667
673
{
668
674
"message": "This is an INFO log",
669
- "service": "shopping-cart-api ",
675
+ "service": "serverlessAirline ",
670
676
"awsRegion": "eu-central-1",
671
677
"correlationIds": {
672
678
"awsRequestId": "c6af9ac6-7b61-11e6-9a41-93e812345678",
@@ -722,7 +728,7 @@ describe('MyUnitTest', () => {
722
728
723
729
test (' Lambda invoked successfully' , async () => {
724
730
725
- const testEvent = { test: ' test' };
731
+ const testEvent = { test: ' test' };
726
732
await handler (testEvent , dummyContext );
727
733
728
734
});
0 commit comments