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
The `Logger` utility must always be instantiated outside of the Lambda handler. In doing this, subsequent invocations processed by the same instance of your function can reuse these resources. This saves cost by reducing function run time. In addition, `Logger` can keep track of a cold start and inject the appropriate fields into logs.
35
+
The `Logger` utility must always be instantiated outside the Lambda handler. By doing this, subsequent invocations processed by the same instance of your function can reuse these resources. This saves cost by reducing function run time. In addition, `Logger` can keep track of a cold start and inject the appropriate fields into logs.
29
36
30
37
=== "handler.ts"
31
38
@@ -45,10 +52,10 @@ The library requires two settings. You can set them as environment variables, or
45
52
46
53
These settings will be used across all logs emitted:
**Logging level** | Sets how verbose Logger should be (INFO, by default). Supported values are: `DEBUG`, `INFO`, `WARN`, `ERROR` | `LOG_LEVEL` | `logLevel`
51
-
**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` | `serviceName`
|**Logging level**| Sets how verbose Logger should be (INFO, by default). Supported values are: `DEBUG`, `INFO`, `WARN`, `ERROR`|`LOG_LEVEL`|`logLevel`|
58
+
|**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`|`serviceName`|
52
59
53
60
For a **complete list** of supported environment variables, refer to [this section](./../index.md#environment-variables).
54
61
@@ -87,15 +94,15 @@ For a **complete list** of supported environment variables, refer to [this secti
87
94
88
95
Your Logger will include the following keys to your structured logging (default log formatter):
**level**: `string` | `INFO` | Logging level set for the Lambda function"s invocation
93
-
**message**: `string` | `Query performed to DynamoDB` | A descriptive, human-readable representation of this log item
94
-
**sampling_rate**: `float`| `0.1` | When enabled, it prints all the logs of a percentage of invocations, e.g. 10%
95
-
**service**: `string` | `serverlessAirline` | A unique name identifier of the service this Lambda function belongs to, by default `service_undefined`
96
-
**timestamp**: `string` | `2011-10-05T14:48:00.000Z` | Timestamp string in simplified extended ISO format (ISO 8601)
97
-
**xray_trace_id**: `string` | `1-5759e988-bd862e3fe1be46a994272793` | X-Ray Trace ID. This value is always presented in Lambda environment, whether [tracing is enabled](https://docs.aws.amazon.com/lambda/latest/dg/services-xray.html){target="_blank"} or not. Logger will always log this value.
98
-
**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
|**level**: `string`|`INFO`| Logging level set for the Lambda function"s invocation|
100
+
|**message**: `string`|`Query performed to DynamoDB`| A descriptive, human-readable representation of this log item|
101
+
|**sampling_rate**: `float`|`0.1`| When enabled, it prints all the logs of a percentage of invocations, e.g. 10%|
102
+
|**service**: `string`|`serverlessAirline`| A unique name identifier of the service this Lambda function belongs to, by default `service_undefined`|
103
+
|**timestamp**: `string`|`2011-10-05T14:48:00.000Z`| Timestamp string in simplified extended ISO format (ISO 8601)|
104
+
|**xray_trace_id**: `string`|`1-5759e988-bd862e3fe1be46a994272793`| X-Ray Trace ID. This value is always presented in Lambda environment, whether [tracing is enabled](https://docs.aws.amazon.com/lambda/latest/dg/services-xray.html){target="_blank"} or not. Logger will always log this value.|
105
+
|**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|
logger.info('This is an INFO log with some context');
173
+
174
+
};
175
+
```
170
176
171
177
In each case, the printed log will look like this:
172
178
@@ -189,7 +195,7 @@ In each case, the printed log will look like this:
189
195
190
196
#### Log incoming event
191
197
192
-
When debugging in non-production environments, you can instruct Logger to log the incoming event with the middleware/decorator parameter `logEvent` or via POWERTOOLS_LOGGER_LOG_EVENT env var set to `true`.
198
+
When debugging in non-production environments, you can instruct Logger to log the incoming event with the middleware/decorator parameter `logEvent` or via `POWERTOOLS_LOGGER_LOG_EVENT` env var set to `true`.
193
199
194
200
???+ warning
195
201
This is disabled by default to prevent sensitive info being logged
@@ -321,7 +329,7 @@ You can manually flush the metrics with `publishStoredMetrics` as follows:
321
329
"Timestamp": 1592234975665,
322
330
"CloudWatchMetrics": [
323
331
{
324
-
"Namespace": "successfulBooking",
332
+
"Namespace": "serverlessAirline",
325
333
"Dimensions": [
326
334
[
327
335
"service"
@@ -333,31 +341,34 @@ You can manually flush the metrics with `publishStoredMetrics` as follows:
333
341
"Unit": "Count"
334
342
}
335
343
]
336
-
}
337
-
]
338
344
},
339
345
"service": "orders"
340
346
}
341
347
```
342
348
343
-
#### Middy middleware
349
+
#### Using the class decorator
344
350
345
-
See below an example of how to automatically flush metrics with the Middy-compatible `logMetrics` middleware.
351
+
!!! info
352
+
Decorators can only be attached to a class declaration, method, accessor, property, or parameter. Therefore, if you prefer to write your handler as a standard function rather than a Class method, check the [middleware](#using-a-middleware) or [manual](#manually) method sections instead.
353
+
See the [official TypeScript documentation](https://www.typescriptlang.org/docs/handbook/decorators.html) for more details.
354
+
355
+
The `logMetrics` decorator of the metrics utility can be used when your Lambda handler function is implemented as method of a Class.
346
356
347
357
=== "handler.ts"
348
358
349
-
```typescript hl_lines="1-2 7 10-11"
350
-
import { Metrics, MetricUnits, logMetrics } from '@aws-lambda-powertools/metrics';
351
-
import middy from '@middy/core';
359
+
```typescript hl_lines="8"
360
+
import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics';
361
+
import { LambdaInterface } from '@aws-lambda-powertools/commons';
352
362
353
363
const metrics = new Metrics({ namespace: 'serverlessAirline', serviceName: 'orders' });
@@ -369,7 +380,7 @@ See below an example of how to automatically flush metrics with the Middy-compat
369
380
"Timestamp": 1592234975665,
370
381
"CloudWatchMetrics": [
371
382
{
372
-
"Namespace": "serverlessAirline",
383
+
"Namespace": "successfulBooking",
373
384
"Dimensions": [
374
385
[
375
386
"service"
@@ -386,29 +397,24 @@ See below an example of how to automatically flush metrics with the Middy-compat
386
397
}
387
398
```
388
399
389
-
#### Using the class decorator
400
+
#### Manually
390
401
391
-
!!! info
392
-
Decorators can only be attached to a class declaration, method, accessor, property, or parameter. Therefore, if you prefer to write your handler as a standard function rather than a Class method, check the [middleware](#using-a-middleware) or [manual](#manually) method sections instead.
393
-
See the [official TypeScript documentation](https://www.typescriptlang.org/docs/handbook/decorators.html) for more details.
402
+
You can manually flush the metrics with `publishStoredMetrics` as follows:
394
403
395
-
The `logMetrics` decorator of the metrics utility can be used when your Lambda handler function is implemented as method of a Class.
404
+
!!! warning
405
+
Metrics, dimensions and namespace validation still applies.
396
406
397
407
=== "handler.ts"
398
408
399
-
```typescript hl_lines="8"
409
+
```typescript hl_lines="7"
400
410
import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics';
401
-
import { LambdaInterface } from '@aws-lambda-powertools/commons';
402
411
403
412
const metrics = new Metrics({ namespace: 'serverlessAirline', serviceName: 'orders' });
404
413
405
-
export class MyFunction implements LambdaInterface {
406
-
407
-
@metrics.logMetrics()
408
-
public async handler(_event: any, _context: any): Promise<void> {
0 commit comments