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
feat(logger): add support for AWS_LAMBDA_LOG_LEVEL and POWERTOOLS_LOG_LEVEL (#1795)
* feat(logger): support advanced logging
* docs(logger): add alc info
* feat(logger): support alc
* docs: fix alc docs links
* tests(logger): add unit tests for the feature
* docs(logger): make POWERTOOLS_LOG_LEVEL default
|**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`|
52
-
|**Logging level**| Sets how verbose Logger should be, from the most verbose to the least verbose (no logs) |`LOG_LEVEL`|`info`|`DEBUG`, `INFO`, `WARN`, `ERROR`, `SILENT`|`ERROR`|`logLevel`|
53
-
|**Log incoming event**| Whether to log or not the incoming event when using the decorator or middleware |`POWERTOOLS_LOGGER_LOG_EVENT`|`false`|`true`, `false`|`false`|`logEvent`|
54
-
|**Debug log sampling**| 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.5`|`sampleRateValue`|
49
+
| 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`|
52
+
|**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`|
53
+
|**Log incoming event**| Whether to log or not the incoming event when using the decorator or middleware |`POWERTOOLS_LOGGER_LOG_EVENT`|`false`|`true`, `false`|`false`|`logEvent`|
54
+
|**Debug log sampling**| 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.5`|`sampleRateValue`|
55
55
56
56
#### Example using AWS Serverless Application Model (SAM)
57
57
@@ -71,7 +71,7 @@ These settings will be used across all logs emitted:
71
71
Runtime: nodejs20.x
72
72
Environment:
73
73
Variables:
74
-
LOG_LEVEL: WARN
74
+
POWERTOOLS_LOG_LEVEL: WARN
75
75
POWERTOOLS_SERVICE_NAME: serverlessAirline
76
76
```
77
77
@@ -394,9 +394,9 @@ The error will be logged with default key name `error`, but you can also pass yo
394
394
395
395
### Log levels
396
396
397
-
The default log level is `INFO` and can be set using the `logLevel` constructor option or by using the `LOG_LEVEL` environment variable.
397
+
The default log level is `INFO` and can be set using the `logLevel` constructor option or by using the `POWERTOOLS_LOG_LEVEL` environment variable.
398
398
399
-
Logger supports the following log levels:
399
+
We support the following log levels:
400
400
401
401
| Level | Numeric value |
402
402
| ---------- | ------------- |
@@ -421,11 +421,48 @@ The `SILENT` log level provides a simple and efficient way to suppress all log m
421
421
422
422
This feature is useful when you want to have your code instrumented to produce logs, but due to some requirement or business decision, you prefer to not emit them.
423
423
424
-
By setting the log level to `SILENT`, which can be done either through the `logLevel` constructor option or by using the `LOG_LEVEL` environment variable, you can easily suppress all logs as needed.
424
+
By setting the log level to `SILENT`, which can be done either through the `logLevel` constructor option or by using the `POWERTOOLS_LOG_LEVEL` environment variable, you can easily suppress all logs as needed.
425
425
426
426
!!! note
427
427
Use the `SILENT` log level with care, as it can make it more challenging to monitor and debug your application. Therefore, we advise using this log level judiciously.
428
428
429
+
#### AWS Lambda Advanced Logging Controls (ALC)
430
+
431
+
With [AWS Lambda Advanced Logging Controls (ALC)](https://docs.aws.amazon.com/lambda/latest/dg/monitoring-cloudwatchlogs.html#monitoring-cloudwatchlogs-advanced), you can control the output format of your logs as either `TEXT` or `JSON` and specify the minimum accepted log level for your application. Regardless of the output format setting in Lambda, we will always output JSON formatted logging messages.
432
+
433
+
When you have this feature enabled, log messages that don’t meet the configured log level are discarded by Lambda. For example, if you set the minimum log level to `WARN`, you will only receive `WARN` and `ERROR` messages in your AWS CloudWatch Logs, all other log levels will be discarded by Lambda.
434
+
435
+
```mermaid
436
+
sequenceDiagram
437
+
title Lambda ALC allows WARN logs only
438
+
participant Lambda service
439
+
participant Lambda function
440
+
participant Application Logger
441
+
442
+
Note over Lambda service: AWS_LAMBDA_LOG_LEVEL="WARN"
**Priority of log level settings in Powertools for AWS Lambda**
455
+
456
+
When the Advanced Logging Controls feature is enabled, we are unable to increase the minimum log level below the `AWS_LAMBDA_LOG_LEVEL` environment variable value, see [AWS Lambda service documentation](https://docs.aws.amazon.com/lambda/latest/dg/monitoring-cloudwatchlogs.html#monitoring-cloudwatchlogs-log-level) for more details.
457
+
458
+
We prioritise log level settings in this order:
459
+
460
+
1.`AWS_LAMBDA_LOG_LEVEL` environment variable
461
+
2. Setting the log level in code using the `logLevel` constructor option, or by calling the `logger.setLogLevel()` method
462
+
3.`POWERTOOLS_LOG_LEVEL` environment variable
463
+
464
+
In the event you have set a log level in Powertools to a level that is lower than the ACL setting, we will output a warning log message informing you that your messages will be discarded by Lambda.
465
+
429
466
### Using multiple Logger instances across your code
430
467
431
468
The `createChild` method allows you to create a child instance of the Logger, which inherits all of the attributes from its parent. You have the option to override any of the settings and attributes from the parent logger, including [its settings](#utility-settings), any [persistent attributes](#appending-persistent-additional-log-keys-and-values), and [the log formatter](#custom-log-formatter-bring-your-own-formatter). Once a child logger is created, the logger and its parent will act as separate instances of the Logger class, and as such any change to one won't be applied to the other.
Copy file name to clipboardExpand all lines: docs/core/metrics.md
+4
Original file line number
Diff line number
Diff line change
@@ -47,6 +47,10 @@ Install the library in your project:
47
47
npm install @aws-lambda-powertools/metrics
48
48
```
49
49
50
+
!!! warning "Caution"
51
+
52
+
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.
53
+
50
54
### Usage
51
55
52
56
The `Metrics` 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, `Metrics` can track cold start and emit the appropriate metrics.
|**POWERTOOLS_DEV**| Increase JSON indentation to ease debugging when running functions locally or in a non-production environment |[Logger](core/logger.md)|`false`|
310
-
|**LOG_LEVEL**| Set logging level |[Logger](core/logger.md)|`INFO`|
310
+
|**POWERTOOLS_LOG_LEVEL**| Sets how verbose Logger should be, from the most verbose to the least verbose (no logs)|[Logger](core/logger.md)|`INFO`|
311
311
|**POWERTOOLS_PARAMETERS_MAX_AGE**| Adjust how long values are kept in cache (in seconds) |[Parameters](utilities/parameters.md)|`5`|
312
312
|**POWERTOOLS_PARAMETERS_SSM_DECRYPT**| Set whether to decrypt or not values retrieved from AWS Systems Manager Parameters Store |[Parameters](utilities/parameters.md)|`false`|
313
313
|**POWERTOOLS_IDEMPOTENCY_DISABLED**| Disable the Idempotency logic without changing your code, useful for testing |[Idempotency](utilities/idempotency.md)|`false`|
`Current log level (%s) does not match AWS Lambda Advanced Logging Controls minimum log level (%s). This can lead to data loss, consider adjusting them.`,
590
+
selectedLogLevel,
591
+
awsLogLevel
592
+
)
593
+
);
594
+
}
595
+
596
+
returntrue;
597
+
}
598
+
599
+
returnfalse;
600
+
}
601
+
573
602
/**
574
603
* It processes a particular log item so that it can be printed to stdout:
575
604
* - Merges ephemeral log attributes with persistent log attributes (printed for all logs) and additional info;
* It returns the value of the `AWS_LAMBDA_LOG_LEVEL` environment variable.
37
+
*
38
+
* The `AWS_LAMBDA_LOG_LEVEL` environment variable is set by AWS Lambda when configuring
39
+
* the function's log level using the Advanced Logging Controls feature. This value always
40
+
* takes precedence over other means of configuring the log level.
41
+
*
42
+
* @note we need to map the `FATAL` log level to `CRITICAL`, see {@link https://docs.aws.amazon.com/lambda/latest/dg/configuration-logging.html#configuration-logging-log-levels AWS Lambda Log Levels}.
0 commit comments