You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
|**Service name**| Sets the name of service of which the Lambda function is part of, that will be present across all log statements |`POWERTOOLS_SERVICE_NAME`|`service_undefined`| Any string |`serverlessAirline`|`serviceName`|
51
-
|**Logging level**| Sets how verbose Logger should be, from the most verbose to the least verbose (no logs) |`POWERTOOLS_LOG_LEVEL`|`INFO`|`DEBUG`, `INFO`, `WARN`, `ERROR`, `CRITICAL`, `SILENT`|`ERROR`|`logLevel`|
52
-
|**Sample rate**| Probability that a Lambda invocation will print all the log items regardless of the log level setting |`POWERTOOLS_LOGGER_SAMPLE_RATE`|`0`|`0.0` to `1.0`|`0.1`|`sampleRateValue`|
48
+
| Setting | Description | Environment variable | Default Value | Allowed Values | Example Value | Constructor parameter |
|**Service name**| Sets the name of service of which the Lambda function is part of, that will be present across all log statements |`POWERTOOLS_SERVICE_NAME`|`service_undefined`| Any string |`serverlessAirline`|`serviceName`|
51
+
|**Logging level**| Sets how verbose Logger should be, from the most verbose to the least verbose (no logs) |`POWERTOOLS_LOG_LEVEL`|`INFO`|`DEBUG`, `INFO`, `WARN`, `ERROR`, `CRITICAL`, `SILENT`|`ERROR`|`logLevel`|
52
+
|**Sample rate**| Probability that a Lambda invocation will print all the log items regardless of the log level setting|`POWERTOOLS_LOGGER_SAMPLE_RATE`|`0`|`0.0` to `1.0`|`0.1`|`sampleRateValue`|
53
53
54
54
See all environment variables in the [Environment variables](../index.md/#environment-variables) section.
55
55
Check API docs to learn more about [Logger constructor options](https://docs.powertools.aws.dev/lambda/typescript/latest/api/types/_aws_lambda_powertools_logger.types.ConstructorOptions.html){target="_blank"}.
@@ -110,10 +110,10 @@ This functionality will include the following keys in your structured logs:
110
110
=== "Middy Middleware"
111
111
112
112
!!! tip "A note about Middy"
113
-
Currently we support only Middy `v3.x` that you can install it by running `npm i @middy/core@~3`.
113
+
We guarantee support only for Middy.js `v4.x`, that you can install it by running `npm i @middy/core@~4`.
114
114
Check their docs to learn more about [Middy and its middleware stack](https://middy.js.org/docs/intro/getting-started){target="_blank"} as well as [best practices when working with Powertools](https://middy.js.org/docs/integrations/lambda-powertools#best-practices){target="_blank"}.
115
115
116
-
```typescript hl_lines="1 13"
116
+
```typescript hl_lines="2 14"
117
117
--8<-- "docs/snippets/logger/middy.ts"
118
118
```
119
119
@@ -131,7 +131,7 @@ This functionality will include the following keys in your structured logs:
131
131
132
132
=== "Manual"
133
133
134
-
```typescript hl_lines="6"
134
+
```typescript hl_lines="10"
135
135
--8<-- "docs/snippets/logger/manual.ts"
136
136
```
137
137
@@ -163,7 +163,7 @@ When debugging in non-production environments, you can instruct Logger to log th
163
163
164
164
=== "Middy Middleware"
165
165
166
-
```typescript hl_lines="10"
166
+
```typescript hl_lines="15"
167
167
--8<-- "docs/snippets/logger/eventMiddy.ts"
168
168
```
169
169
@@ -236,7 +236,7 @@ If you want to make sure that persistent attributes added **inside the handler f
236
236
237
237
=== "Middy Middleware"
238
238
239
-
```typescript hl_lines="30"
239
+
```typescript hl_lines="31"
240
240
--8<-- "docs/snippets/logger/clearStateMiddy.ts"
241
241
```
242
242
@@ -509,23 +509,21 @@ The `createChild` method allows you to create a child instance of the Logger, wh
509
509
}
510
510
```
511
511
512
-
### Sampling logs
512
+
### Sampling debug logs
513
513
514
-
Use sampling when you want to print all the log items generated in your code, based on a **percentage of your concurrent/cold start invocations**.
514
+
Use sampling when you want to dynamically change your log level to **DEBUG** based on a **percentage of your concurrent/cold start invocations**.
515
515
516
-
You can do that by setting a "sample rate", a float value ranging from `0.0` (0%) to `1` (100%), by using a `POWERTOOLS_LOGGER_SAMPLE_RATE` env var or passing the `sampleRateValue` parameter in the Logger constructor.
517
-
This number represents the probability that a Lambda invocation will print all the log items regardless of the log level setting.
518
-
519
-
For example, by setting the "sample rate" to `0.5`, roughly 50% of your lambda invocations will print all the log items, including the `debug` ones.
516
+
You can use values ranging from `0` to `1` (100%) when setting the `sampleRateValue` constructor option or `POWERTOOLS_LOGGER_SAMPLE_RATE` env var.
520
517
521
518
!!! tip "When is this useful?"
522
-
In production, to avoid log data pollution and reduce CloudWatch costs, developers are encouraged to use the logger with `logLevel` equal to `ERROR` or `WARN`.
523
-
This means that only errors or warnings will be printed.
519
+
Let's imagine a sudden spike increase in concurrency triggered a transient issue downstream. When looking into the logs you might not have enough information, and while you can adjust log levels it might not happen again.
524
520
525
-
However, it might still be useful to print all the logs (including debug ones) of a very small percentage of invocations to have a better understanding of the behaviour of your code in production even when there are no errors.
526
-
527
-
**Sampling decision happens at the Logger initialization**. This means sampling may happen significantly more or less than depending on your traffic patterns, for example a steady low number of invocations and thus few cold starts.
528
-
If you want to reset the sampling decision and refresh it for each invocation, you can call the `logger.refreshSampleRateCalculation()` method at the beginning or end of your handler.
521
+
This feature takes into account transient issues where additional debugging information can be useful.
522
+
523
+
Sampling decision happens at the Logger initialization. This means sampling may happen significantly more or less than depending on your traffic patterns, for example a steady low number of invocations and thus few cold starts.
524
+
525
+
!!! note
526
+
Open a [feature request](https://github.com/aws-powertools/powertools-lambda-typescript/issues/new?assignees=&labels=type%2Ffeature-request%2Ctriage&projects=aws-powertools%2F7&template=feature_request.yml&title=Feature+request%3A+TITLE) if you want Logger to calculate sampling for every invocation
529
527
530
528
=== "handler.ts"
531
529
@@ -639,7 +637,7 @@ You can customize the structure (keys and values) of your log items by passing a
@@ -700,9 +698,6 @@ This is a Jest sample that provides the minimum information necessary for Logger
700
698
--8<-- "docs/snippets/logger/unitTesting.ts"
701
699
```
702
700
703
-
!!! tip
704
-
If you don't want to declare your own dummy Lambda Context, you can use [`ContextExamples.helloworldContext`](https://github.com/aws-powertools/powertools-lambda-typescript/blob/main/packages/commons/src/samples/resources/contexts/hello-world.ts#L3-L16) from [`@aws-lambda-powertools/commons`](https://www.npmjs.com/package/@aws-lambda-powertools/commons).
705
-
706
701
### Suppress logs with Jest
707
702
708
703
When unit testing your code with [Jest](https://jestjs.io) you can use the `POWERTOOLS_DEV` environment variable in conjunction with the Jest `--silent` CLI option to suppress logs from Logger.
Using the Lambda [Advanced Logging Controls](...docs link) feature requires you to update your version of Powertools for AWS Lambda (TypeScript) to at least v1.15.0 to ensure metrics are reported correctly to Amazon CloudWatch Metrics.
52
+
Using the Lambda [Advanced Logging Controls](https://docs.aws.amazon.com/lambda/latest/dg/monitoring-cloudwatchlogs.html#monitoring-cloudwatchlogs-advanced) feature requires you to update your version of Powertools for AWS Lambda (TypeScript) to at least v1.15.0 to ensure metrics are reported correctly to Amazon CloudWatch Metrics.
53
53
54
54
### Usage
55
55
@@ -253,7 +253,7 @@ See below an example of how to automatically flush metrics with the Middy-compat
253
253
254
254
=== "handler.ts"
255
255
256
-
```typescript hl_lines="20"
256
+
```typescript hl_lines="2 17"
257
257
--8<-- "docs/snippets/metrics/middy.ts"
258
258
```
259
259
@@ -368,7 +368,7 @@ You can optionally capture cold start metrics with the `logMetrics` middleware o
Copy file name to clipboardExpand all lines: docs/core/tracer.md
+4-4
Original file line number
Diff line number
Diff line change
@@ -88,10 +88,10 @@ You can quickly start by importing the `Tracer` class, initialize it outside the
88
88
=== "Middy Middleware"
89
89
90
90
!!! tip "A note about Middy"
91
-
Currently we support only Middy `v3.x` that you can install it by running `npm i @middy/core@~3`.
91
+
We guarantee support only for Middy.js `v4.x`, that you can install it by running `npm i @middy/core@~4`.
92
92
Check their docs to learn more about [Middy and its middleware stack](https://middy.js.org/docs/intro/getting-started){target="_blank"} as well as [best practices when working with Powertools](https://middy.js.org/docs/integrations/lambda-powertools#best-practices){target="_blank"}.
93
93
94
-
```typescript hl_lines="1 14 16"
94
+
```typescript hl_lines="2 15 17"
95
95
--8<-- "docs/snippets/tracer/middy.ts"
96
96
```
97
97
@@ -197,7 +197,7 @@ You can patch all AWS SDK v2 clients by calling the `captureAWS` method:
197
197
198
198
=== "index.ts"
199
199
200
-
```typescript hl_lines="6"
200
+
```typescript hl_lines="7"
201
201
--8<-- "docs/snippets/tracer/captureAWSAll.ts"
202
202
```
203
203
@@ -284,7 +284,7 @@ Alternatively, use the `captureResponse: false` option in both `tracer.captureLa
0 commit comments