@@ -456,63 +456,131 @@ For example, by setting the "sample rate" to `0.5`, roughly 50% of your lambda i
456
456
457
457
=== "handler.ts"
458
458
459
- ```typescript hl_lines="5 "
459
+ ```typescript hl_lines="6 "
460
460
import { Logger } from "@aws-lambda-powertools/logger";
461
461
462
+ // Notice the log level set to 'ERROR'
462
463
const logger = new Logger({
463
464
logLevel: "ERROR",
464
465
sampleRateValue: 0.5
465
466
});
466
467
467
468
const lambdaHandler = async () => {
468
-
469
- // 0.5 means that you have 50% chance that these logs will be printed
470
- logger.info("This is INFO log #1");
471
- logger.info("This is INFO log #2");
472
- logger.info("This is INFO log #3");
473
- logger.info("This is INFO log #4");
469
+
470
+ // This log item (equal to log level 'ERROR') will be printed to standard output
471
+ // in all Lambda invocations
472
+ logger.error("This is an ERROR log");
473
+
474
+ // These log items (below the log level 'ERROR') have ~50% chance
475
+ // of being printed in a Lambda invocation
476
+ logger.debug("This is a DEBUG log that has 50% chance of being printed");
477
+ logger.info("This is an INFO log that has 50% chance of being printed");
478
+ logger.warn("This is a WARN log that has 50% chance of being printed");
474
479
475
480
// Optional: refresh sample rate calculation on runtime
476
481
// logger.refreshSampleRateCalculation();
477
482
478
483
};
479
484
```
480
485
481
- === "Example CloudWatch Logs excerpt"
486
+ === "Example CloudWatch Logs excerpt - Invocation # 1 "
482
487
483
- ```json hl_lines="4 12 20 28"
488
+ ```json
484
489
{
485
- "level": "INFO ",
486
- "message": "This is INFO log #1 ",
490
+ "level": "ERROR ",
491
+ "message": "This is an ERROR log ",
487
492
"sampling_rate": "0.5",
488
493
"service": "shopping-cart-api",
489
494
"timestamp": "2021-12-12T22:59:06.334Z",
490
495
"xray_trace_id": "abcdef123456abcdef123456abcdef123456"
491
496
}
492
497
{
493
- "level": "INFO ",
494
- "message": "This is INFO log #2 ",
498
+ "level": "DEBUG ",
499
+ "message": "This is a DEBUG log that has 50% chance of being printed ",
495
500
"sampling_rate": "0.5",
496
501
"service": "shopping-cart-api",
497
502
"timestamp": "2021-12-12T22:59:06.337Z",
498
503
"xray_trace_id": "abcdef123456abcdef123456abcdef123456"
499
504
}
500
505
{
501
506
"level": "INFO",
502
- "message": "This is INFO log #3 ",
507
+ "message": "This is an INFO log that has 50% chance of being printed ",
503
508
"sampling_rate": "0.5",
504
509
"service": "shopping-cart-api",
505
510
"timestamp": "2021-12-12T22:59:06.338Z",
506
511
"xray_trace_id": "abcdef123456abcdef123456abcdef123456"
507
512
}
513
+ {
514
+ "level": "WARN",
515
+ "message": "This is a WARN log that has 50% chance of being printed",
516
+ "sampling_rate": "0.5",
517
+ "service": "shopping-cart-api",
518
+ "timestamp": "2021-12-12T22:59:06.338Z",
519
+ "xray_trace_id": "abcdef123456abcdef123456abcdef123456"
520
+ }
521
+ ```
522
+
523
+ === "Example CloudWatch Logs excerpt - Invocation #2 "
524
+
525
+ ```json
526
+ {
527
+ "level": "ERROR",
528
+ "message": "This is an ERROR log",
529
+ "sampling_rate": "0.5",
530
+ "service": "shopping-cart-api",
531
+ "timestamp": "2021-12-12T22:59:06.334Z",
532
+ "xray_trace_id": "abcdef123456abcdef123456abcdef123456"
533
+ }
534
+ ```
535
+
536
+ === "Example CloudWatch Logs excerpt - Invocation #3 "
537
+
538
+ ```json
539
+ {
540
+ "level": "ERROR",
541
+ "message": "This is an ERROR log",
542
+ "sampling_rate": "0.5",
543
+ "service": "shopping-cart-api",
544
+ "timestamp": "2021-12-12T22:59:06.334Z",
545
+ "xray_trace_id": "abcdef123456abcdef123456abcdef123456"
546
+ }
547
+ {
548
+ "level": "DEBUG",
549
+ "message": "This is a DEBUG log that has 50% chance of being printed",
550
+ "sampling_rate": "0.5",
551
+ "service": "shopping-cart-api",
552
+ "timestamp": "2021-12-12T22:59:06.337Z",
553
+ "xray_trace_id": "abcdef123456abcdef123456abcdef123456"
554
+ }
508
555
{
509
556
"level": "INFO",
510
- "message": "This is INFO log #4 ",
557
+ "message": "This is an INFO log that has 50% chance of being printed ",
511
558
"sampling_rate": "0.5",
512
559
"service": "shopping-cart-api",
513
560
"timestamp": "2021-12-12T22:59:06.338Z",
514
561
"xray_trace_id": "abcdef123456abcdef123456abcdef123456"
515
562
}
563
+ {
564
+ "level": "WARN",
565
+ "message": "This is a WARN log that has 50% chance of being printed",
566
+ "sampling_rate": "0.5",
567
+ "service": "shopping-cart-api",
568
+ "timestamp": "2021-12-12T22:59:06.338Z",
569
+ "xray_trace_id": "abcdef123456abcdef123456abcdef123456"
570
+ }
571
+ ```
572
+
573
+ === "Example CloudWatch Logs excerpt - Invocation #4 "
574
+
575
+ ```json
576
+ {
577
+ "level": "ERROR",
578
+ "message": "This is an ERROR log",
579
+ "sampling_rate": "0.5",
580
+ "service": "shopping-cart-api",
581
+ "timestamp": "2021-12-12T22:59:06.334Z",
582
+ "xray_trace_id": "abcdef123456abcdef123456abcdef123456"
583
+ }
516
584
```
517
585
518
586
### Custom Log formatter (Bring Your Own Formatter)
0 commit comments