Skip to content

Commit a8594b9

Browse files
authored
docs(logger): improve mkdocs and examples of sample rate feature (#389)
1 parent 4f1b8ba commit a8594b9

File tree

2 files changed

+95
-22
lines changed

2 files changed

+95
-22
lines changed

docs/core/logger.md

+83-15
Original file line numberDiff line numberDiff line change
@@ -456,63 +456,131 @@ For example, by setting the "sample rate" to `0.5`, roughly 50% of your lambda i
456456

457457
=== "handler.ts"
458458

459-
```typescript hl_lines="5"
459+
```typescript hl_lines="6"
460460
import { Logger } from "@aws-lambda-powertools/logger";
461461

462+
// Notice the log level set to 'ERROR'
462463
const logger = new Logger({
463464
logLevel: "ERROR",
464465
sampleRateValue: 0.5
465466
});
466467

467468
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");
474479
475480
// Optional: refresh sample rate calculation on runtime
476481
// logger.refreshSampleRateCalculation();
477482

478483
};
479484
```
480485

481-
=== "Example CloudWatch Logs excerpt"
486+
=== "Example CloudWatch Logs excerpt - Invocation #1"
482487

483-
```json hl_lines="4 12 20 28"
488+
```json
484489
{
485-
"level": "INFO",
486-
"message": "This is INFO log #1",
490+
"level": "ERROR",
491+
"message": "This is an ERROR log",
487492
"sampling_rate": "0.5",
488493
"service": "shopping-cart-api",
489494
"timestamp": "2021-12-12T22:59:06.334Z",
490495
"xray_trace_id": "abcdef123456abcdef123456abcdef123456"
491496
}
492497
{
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",
495500
"sampling_rate": "0.5",
496501
"service": "shopping-cart-api",
497502
"timestamp": "2021-12-12T22:59:06.337Z",
498503
"xray_trace_id": "abcdef123456abcdef123456abcdef123456"
499504
}
500505
{
501506
"level": "INFO",
502-
"message": "This is INFO log #3",
507+
"message": "This is an INFO log that has 50% chance of being printed",
503508
"sampling_rate": "0.5",
504509
"service": "shopping-cart-api",
505510
"timestamp": "2021-12-12T22:59:06.338Z",
506511
"xray_trace_id": "abcdef123456abcdef123456abcdef123456"
507512
}
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+
}
508555
{
509556
"level": "INFO",
510-
"message": "This is INFO log #4",
557+
"message": "This is an INFO log that has 50% chance of being printed",
511558
"sampling_rate": "0.5",
512559
"service": "shopping-cart-api",
513560
"timestamp": "2021-12-12T22:59:06.338Z",
514561
"xray_trace_id": "abcdef123456abcdef123456abcdef123456"
515562
}
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+
}
516584
```
517585

518586
### Custom Log formatter (Bring Your Own Formatter)

packages/logger/examples/sample-rate.ts

+12-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
require('./../tests/helpers/populateEnvironmentVariables');
33

44
// Additional runtime variables
5-
process.env.LOG_LEVEL = 'WARN';
5+
process.env.LOG_LEVEL = 'ERROR';
66
process.env.POWERTOOLS_SERVICE_NAME = 'hello-world';
77
process.env.POWERTOOLS_LOGGER_SAMPLE_RATE = '0.5';
88

@@ -15,13 +15,18 @@ const logger = new Logger();
1515

1616
const lambdaHandler: Handler = async () => {
1717

18-
logger.info('This is INFO log #1');
19-
logger.info('This is INFO log #2');
20-
logger.info('This is INFO log #3');
21-
logger.info('This is INFO log #4');
18+
// This log item (equal to log level 'ERROR') will be printed to standard output
19+
// in all Lambda invocations
20+
logger.error('This is an ERROR log');
2221

23-
// Refresh sample rate calculation on runtime
24-
logger.refreshSampleRateCalculation();
22+
// These log items (below the log level 'ERROR') have ~50% chance
23+
// of being printed in a Lambda invocation
24+
logger.debug('This is a DEBUG log that has 50% chance of being printed');
25+
logger.info('This is an INFO log that has 50% chance of being printed');
26+
logger.warn('This is a WARN log that has 50% chance of being printed');
27+
28+
// Optional: refresh sample rate calculation on runtime
29+
// logger.refreshSampleRateCalculation();
2530

2631
return {
2732
foo: 'bar'

0 commit comments

Comments
 (0)