UNPKG

@aws-lambda-powertools/logger

Version:
108 lines (107 loc) 4.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.EnvironmentVariablesService = void 0; const commons_1 = require("@aws-lambda-powertools/commons"); /** * Class EnvironmentVariablesService * * This class is used to return environment variables that are available in the runtime of * the current Lambda invocation. * These variables can be a mix of runtime environment variables set by AWS and * variables that can be set by the developer additionally. * * @see https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html#configuration-envvars-runtime * @see https://docs.powertools.aws.dev/lambda/typescript/latest/#environment-variables */ class EnvironmentVariablesService extends commons_1.EnvironmentVariablesService { // Reserved environment variables awsLogLevelVariable = 'AWS_LAMBDA_LOG_LEVEL'; awsRegionVariable = 'AWS_REGION'; currentEnvironmentVariable = 'ENVIRONMENT'; functionNameVariable = 'AWS_LAMBDA_FUNCTION_NAME'; functionVersionVariable = 'AWS_LAMBDA_FUNCTION_VERSION'; logEventVariable = 'POWERTOOLS_LOGGER_LOG_EVENT'; logLevelVariable = 'POWERTOOLS_LOG_LEVEL'; logLevelVariableLegacy = 'LOG_LEVEL'; memoryLimitInMBVariable = 'AWS_LAMBDA_FUNCTION_MEMORY_SIZE'; sampleRateValueVariable = 'POWERTOOLS_LOGGER_SAMPLE_RATE'; tzVariable = 'TZ'; /** * Return 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. * * 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}. */ getAwsLogLevel() { const awsLogLevelVariable = this.get(this.awsLogLevelVariable); return awsLogLevelVariable === 'FATAL' ? 'CRITICAL' : awsLogLevelVariable; } /** * Return the value of the AWS_REGION environment variable. */ getAwsRegion() { return this.get(this.awsRegionVariable); } /** * Return the value of the ENVIRONMENT environment variable. */ getCurrentEnvironment() { return this.get(this.currentEnvironmentVariable); } /** * Return the value of the AWS_LAMBDA_FUNCTION_MEMORY_SIZE environment variable. */ getFunctionMemory() { const value = this.get(this.memoryLimitInMBVariable); return Number(value); } /** * Return the value of the AWS_LAMBDA_FUNCTION_NAME environment variable. */ getFunctionName() { return this.get(this.functionNameVariable); } /** * Return the value of the AWS_LAMBDA_FUNCTION_VERSION environment variable. */ getFunctionVersion() { return this.get(this.functionVersionVariable); } /** * Return the value of the POWERTOOLS_LOGGER_LOG_EVENT environment variable. */ getLogEvent() { const value = this.get(this.logEventVariable); return this.isValueTrue(value); } /** * Return the value of the `POWERTOOLS_LOG_LEVEL` or `LOG_LEVEL` (legacy) environment variables * when the first one is not set. * * The `LOG_LEVEL` environment variable is considered legacy and will be removed in a future release. * The `AWS_LAMBDA_LOG_LEVEL` environment variable always takes precedence over the ones above. */ getLogLevel() { const logLevelVariable = this.get(this.logLevelVariable); const logLevelVariableAlias = this.get(this.logLevelVariableLegacy); return logLevelVariable !== '' ? logLevelVariable : logLevelVariableAlias; } /** * Return the value of the POWERTOOLS_LOGGER_SAMPLE_RATE environment variable. */ getSampleRateValue() { const value = this.get(this.sampleRateValueVariable); return value && value.length > 0 ? Number(value) : undefined; } /** * Return the value of the `TZ` environment variable or `UTC` if it is not set. */ getTimezone() { const value = this.get(this.tzVariable); return value.length > 0 ? value : 'UTC'; } } exports.EnvironmentVariablesService = EnvironmentVariablesService;