@aws-lambda-powertools/logger
Advanced tools
Comparing version 1.15.0 to 1.16.0
@@ -17,2 +17,14 @@ /** | ||
/** | ||
* It returns the value of the `AWS_LAMBDA_LOG_LEVEL` environment variable. | ||
* | ||
* The `AWS_LAMBDA_LOG_LEVEL` environment variable is set by AWS Lambda when configuring | ||
* the function's log level using the Advanced Logging Controls feature. This value always | ||
* takes precedence over other means of configuring the log level. | ||
* | ||
* @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}. | ||
* | ||
* @returns {string} | ||
*/ | ||
getAwsLogLevel(): string; | ||
/** | ||
* It returns the value of the ENVIRONMENT environment variable. | ||
@@ -30,4 +42,8 @@ * | ||
/** | ||
* It returns the value of the LOG_LEVEL environment variable. | ||
* It returns the value of the `POWERTOOLS_LOG_LEVEL, or `LOG_LEVEL` (legacy) environment variables | ||
* when the first one is not set. | ||
* | ||
* @note The `LOG_LEVEL` environment variable is considered legacy and will be removed in a future release. | ||
* @note The `AWS_LAMBDA_LOG_LEVEL` environment variable always takes precedence over the ones above. | ||
* | ||
* @returns {string} | ||
@@ -34,0 +50,0 @@ */ |
@@ -18,2 +18,3 @@ import { ConfigServiceInterface } from './ConfigServiceInterface'; | ||
declare class EnvironmentVariablesService extends CommonEnvironmentVariablesService implements ConfigServiceInterface { | ||
private awsLogLevelVariable; | ||
private awsRegionVariable; | ||
@@ -25,5 +26,18 @@ private currentEnvironmentVariable; | ||
private logLevelVariable; | ||
private logLevelVariableLegacy; | ||
private memoryLimitInMBVariable; | ||
private sampleRateValueVariable; | ||
/** | ||
* It returns the value of the `AWS_LAMBDA_LOG_LEVEL` environment variable. | ||
* | ||
* The `AWS_LAMBDA_LOG_LEVEL` environment variable is set by AWS Lambda when configuring | ||
* the function's log level using the Advanced Logging Controls feature. This value always | ||
* takes precedence over other means of configuring the log level. | ||
* | ||
* @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}. | ||
* | ||
* @returns {string} | ||
*/ | ||
getAwsLogLevel(): string; | ||
/** | ||
* It returns the value of the AWS_REGION environment variable. | ||
@@ -65,4 +79,8 @@ * | ||
/** | ||
* It returns the value of the LOG_LEVEL environment variable. | ||
* It returns the value of the `POWERTOOLS_LOG_LEVEL, or `LOG_LEVEL` (legacy) environment variables | ||
* when the first one is not set. | ||
* | ||
* @note The `LOG_LEVEL` environment variable is considered legacy and will be removed in a future release. | ||
* @note The `AWS_LAMBDA_LOG_LEVEL` environment variable always takes precedence over the ones above. | ||
* | ||
* @returns {string} | ||
@@ -69,0 +87,0 @@ */ |
@@ -23,2 +23,3 @@ "use strict"; | ||
// Reserved environment variables | ||
this.awsLogLevelVariable = 'AWS_LAMBDA_LOG_LEVEL'; | ||
this.awsRegionVariable = 'AWS_REGION'; | ||
@@ -29,3 +30,4 @@ this.currentEnvironmentVariable = 'ENVIRONMENT'; | ||
this.logEventVariable = 'POWERTOOLS_LOGGER_LOG_EVENT'; | ||
this.logLevelVariable = 'LOG_LEVEL'; | ||
this.logLevelVariable = 'POWERTOOLS_LOG_LEVEL'; | ||
this.logLevelVariableLegacy = 'LOG_LEVEL'; | ||
this.memoryLimitInMBVariable = 'AWS_LAMBDA_FUNCTION_MEMORY_SIZE'; | ||
@@ -35,2 +37,17 @@ this.sampleRateValueVariable = 'POWERTOOLS_LOGGER_SAMPLE_RATE'; | ||
/** | ||
* It returns the value of the `AWS_LAMBDA_LOG_LEVEL` environment variable. | ||
* | ||
* The `AWS_LAMBDA_LOG_LEVEL` environment variable is set by AWS Lambda when configuring | ||
* the function's log level using the Advanced Logging Controls feature. This value always | ||
* takes precedence over other means of configuring the log level. | ||
* | ||
* @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}. | ||
* | ||
* @returns {string} | ||
*/ | ||
getAwsLogLevel() { | ||
const awsLogLevelVariable = this.get(this.awsLogLevelVariable); | ||
return awsLogLevelVariable === 'FATAL' ? 'CRITICAL' : awsLogLevelVariable; | ||
} | ||
/** | ||
* It returns the value of the AWS_REGION environment variable. | ||
@@ -86,8 +103,14 @@ * | ||
/** | ||
* It returns the value of the LOG_LEVEL environment variable. | ||
* It returns the value of the `POWERTOOLS_LOG_LEVEL, or `LOG_LEVEL` (legacy) environment variables | ||
* when the first one is not set. | ||
* | ||
* @note The `LOG_LEVEL` environment variable is considered legacy and will be removed in a future release. | ||
* @note The `AWS_LAMBDA_LOG_LEVEL` environment variable always takes precedence over the ones above. | ||
* | ||
* @returns {string} | ||
*/ | ||
getLogLevel() { | ||
return this.get(this.logLevelVariable); | ||
const logLevelVariable = this.get(this.logLevelVariable); | ||
const logLevelVariableAlias = this.get(this.logLevelVariableLegacy); | ||
return logLevelVariable !== '' ? logLevelVariable : logLevelVariableAlias; | ||
} | ||
@@ -94,0 +117,0 @@ /** |
@@ -294,2 +294,5 @@ import type { Context } from 'aws-lambda'; | ||
* | ||
* If the log level is set using AWS Lambda Advanced Logging Controls, it sets it | ||
* instead of the given log level to avoid data loss. | ||
* | ||
* @param logLevel The log level to set, i.e. `error`, `warn`, `info`, `debug`, etc. | ||
@@ -358,2 +361,3 @@ */ | ||
private addToPowertoolLogData; | ||
private awsLogLevelShortCircuit; | ||
/** | ||
@@ -479,7 +483,8 @@ * It processes a particular log item so that it can be printed to stdout: | ||
* Sets the initial Logger log level based on the following order: | ||
* 1. If a log level is passed to the constructor, it sets it. | ||
* 2. If a log level is set via custom config service, it sets it. | ||
* 3. If a log level is set via env variables, it sets it. | ||
* 1. If a log level is set using AWS Lambda Advanced Logging Controls, it sets it. | ||
* 2. If a log level is passed to the constructor, it sets it. | ||
* 3. If a log level is set via custom config service, it sets it. | ||
* 4. If a log level is set via env variables, it sets it. | ||
* | ||
* If none of the above is true, the default log level applies (INFO). | ||
* If none of the above is true, the default log level applies (`INFO`). | ||
* | ||
@@ -486,0 +491,0 @@ * @private |
@@ -9,2 +9,3 @@ "use strict"; | ||
const node_console_1 = require("node:console"); | ||
const node_util_1 = require("node:util"); | ||
const commons_1 = require("@aws-lambda-powertools/commons"); | ||
@@ -406,5 +407,10 @@ const formatter_1 = require("./formatter"); | ||
* | ||
* If the log level is set using AWS Lambda Advanced Logging Controls, it sets it | ||
* instead of the given log level to avoid data loss. | ||
* | ||
* @param logLevel The log level to set, i.e. `error`, `warn`, `info`, `debug`, etc. | ||
*/ | ||
setLogLevel(logLevel) { | ||
if (this.awsLogLevelShortCircuit(logLevel)) | ||
return; | ||
if (this.isValidLogLevel(logLevel)) { | ||
@@ -502,2 +508,14 @@ this.logLevel = this.logLevelThresholds[logLevel]; | ||
} | ||
awsLogLevelShortCircuit(selectedLogLevel) { | ||
const awsLogLevel = this.getEnvVarsService().getAwsLogLevel(); | ||
if (this.isValidLogLevel(awsLogLevel)) { | ||
this.logLevel = this.logLevelThresholds[awsLogLevel]; | ||
if (this.isValidLogLevel(selectedLogLevel) && | ||
this.logLevel > this.logLevelThresholds[selectedLogLevel]) { | ||
this.warn((0, node_util_1.format)(`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.`, selectedLogLevel, awsLogLevel)); | ||
} | ||
return true; | ||
} | ||
return false; | ||
} | ||
/** | ||
@@ -715,7 +733,8 @@ * It processes a particular log item so that it can be printed to stdout: | ||
* Sets the initial Logger log level based on the following order: | ||
* 1. If a log level is passed to the constructor, it sets it. | ||
* 2. If a log level is set via custom config service, it sets it. | ||
* 3. If a log level is set via env variables, it sets it. | ||
* 1. If a log level is set using AWS Lambda Advanced Logging Controls, it sets it. | ||
* 2. If a log level is passed to the constructor, it sets it. | ||
* 3. If a log level is set via custom config service, it sets it. | ||
* 4. If a log level is set via env variables, it sets it. | ||
* | ||
* If none of the above is true, the default log level applies (INFO). | ||
* If none of the above is true, the default log level applies (`INFO`). | ||
* | ||
@@ -727,2 +746,4 @@ * @private | ||
const constructorLogLevel = logLevel?.toUpperCase(); | ||
if (this.awsLogLevelShortCircuit(constructorLogLevel)) | ||
return; | ||
if (this.isValidLogLevel(constructorLogLevel)) { | ||
@@ -729,0 +750,0 @@ this.logLevel = this.logLevelThresholds[constructorLogLevel]; |
{ | ||
"name": "@aws-lambda-powertools/logger", | ||
"version": "1.15.0", | ||
"version": "1.16.0", | ||
"description": "The logging package for the Powertools for AWS Lambda (TypeScript) library", | ||
@@ -28,3 +28,3 @@ "author": { | ||
"dependencies": { | ||
"@aws-lambda-powertools/commons": "^1.15.0", | ||
"@aws-lambda-powertools/commons": "^1.16.0", | ||
"lodash.merge": "^4.6.2" | ||
@@ -31,0 +31,0 @@ }, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
107822
2398