@@ -6,9 +6,15 @@ import type {
6
6
HandlerMethodDecorator ,
7
7
SyncHandler ,
8
8
} from '@aws-lambda-powertools/commons/types' ;
9
+ import {
10
+ getBooleanFromEnv ,
11
+ getNumberFromEnv ,
12
+ getStringFromEnv ,
13
+ getXRayTraceIdFromEnv ,
14
+ isDevMode ,
15
+ } from '@aws-lambda-powertools/commons/utils/env' ;
9
16
import type { Context , Handler } from 'aws-lambda' ;
10
17
import merge from 'lodash.merge' ;
11
- import { EnvironmentVariablesService } from './config/EnvironmentVariablesService.js' ;
12
18
import {
13
19
LogJsonIndent ,
14
20
LogLevelThreshold ,
@@ -112,10 +118,6 @@ class Logger extends Utility implements LoggerInterface {
112
118
* Custom config service instance used to configure the logger.
113
119
*/
114
120
private customConfigService ?: ConfigServiceInterface ;
115
- /**
116
- * Environment variables service instance used to fetch environment variables.
117
- */
118
- private envVarsService = new EnvironmentVariablesService ( ) ;
119
121
/**
120
122
* Whether to print the Lambda invocation event in the logs.
121
123
*/
@@ -850,7 +852,7 @@ class Logger extends Utility implements LoggerInterface {
850
852
const unformattedBaseAttributes = {
851
853
logLevel : this . getLogLevelNameFromNumber ( logLevel ) ,
852
854
timestamp : new Date ( ) ,
853
- xRayTraceId : this . envVarsService . getXrayTraceId ( ) ,
855
+ xRayTraceId : getXRayTraceIdFromEnv ( ) ,
854
856
...this . getPowertoolsLogData ( ) ,
855
857
message : '' ,
856
858
} ;
@@ -975,13 +977,6 @@ class Logger extends Utility implements LoggerInterface {
975
977
return this . customConfigService ;
976
978
}
977
979
978
- /**
979
- * Get the instance of a service that fetches environment variables.
980
- */
981
- private getEnvVarsService ( ) : EnvironmentVariablesService {
982
- return this . envVarsService as EnvironmentVariablesService ;
983
- }
984
-
985
980
/**
986
981
* Get the instance of a service that formats the structure of a
987
982
* log item's keys and values in the desired way.
@@ -1081,7 +1076,7 @@ class Logger extends Utility implements LoggerInterface {
1081
1076
input : LogItemMessage ,
1082
1077
extraInput : LogItemExtraInput
1083
1078
) : void {
1084
- const traceId = this . envVarsService . getXrayTraceId ( ) ;
1079
+ const traceId = getXRayTraceIdFromEnv ( ) ;
1085
1080
if ( traceId !== undefined && this . shouldBufferLog ( traceId , logLevel ) ) {
1086
1081
try {
1087
1082
this . bufferLogItem (
@@ -1125,7 +1120,7 @@ class Logger extends Utility implements LoggerInterface {
1125
1120
* or as the global node console if the `POWERTOOLS_DEV' env variable is set and has truthy value.
1126
1121
*/
1127
1122
private setConsole ( ) : void {
1128
- if ( ! this . getEnvVarsService ( ) . isDevMode ( ) ) {
1123
+ if ( ! isDevMode ( ) ) {
1129
1124
this . console = new Console ( {
1130
1125
stdout : process . stdout ,
1131
1126
stderr : process . stderr ,
@@ -1190,9 +1185,21 @@ class Logger extends Utility implements LoggerInterface {
1190
1185
1191
1186
return ;
1192
1187
}
1193
- const envVarsValue = this . getEnvVarsService ( ) ?. getLogLevel ( ) ?. toUpperCase ( ) ;
1194
- if ( this . isValidLogLevel ( envVarsValue ) ) {
1195
- this . logLevel = LogLevelThreshold [ envVarsValue ] ;
1188
+
1189
+ const logLevelVariable = getStringFromEnv ( {
1190
+ key : 'POWERTOOLS_LOG_LEVEL' ,
1191
+ defaultValue : '' ,
1192
+ } ) ;
1193
+ const logLevelVariableAlias = getStringFromEnv ( {
1194
+ key : 'LOG_LEVEL' ,
1195
+ defaultValue : '' ,
1196
+ } ) ;
1197
+
1198
+ const logLevelValue =
1199
+ logLevelVariable !== '' ? logLevelVariable : logLevelVariableAlias ;
1200
+
1201
+ if ( this . isValidLogLevel ( logLevelValue ) ) {
1202
+ this . logLevel = LogLevelThreshold [ logLevelValue ] ;
1196
1203
this . #initialLogLevel = this . logLevel ;
1197
1204
1198
1205
return ;
@@ -1212,8 +1219,15 @@ class Logger extends Utility implements LoggerInterface {
1212
1219
const constructorValue = sampleRateValue ;
1213
1220
const customConfigValue =
1214
1221
this . getCustomConfigService ( ) ?. getSampleRateValue ( ) ;
1215
- const envVarsValue = this . getEnvVarsService ( ) . getSampleRateValue ( ) ;
1216
- for ( const value of [ constructorValue , customConfigValue , envVarsValue ] ) {
1222
+ const sampleRateEnvVariable = getNumberFromEnv ( {
1223
+ key : 'POWERTOOLS_LOGGER_SAMPLE_RATE' ,
1224
+ defaultValue : 0 ,
1225
+ } ) ;
1226
+ for ( const value of [
1227
+ constructorValue ,
1228
+ customConfigValue ,
1229
+ sampleRateEnvVariable ,
1230
+ ] ) {
1217
1231
if ( this . isValidSampleRate ( value ) ) {
1218
1232
this . #debugLogSampling. sampleRateValue = value ;
1219
1233
this . powertoolsLogData . sampleRateValue = value ;
@@ -1236,9 +1250,10 @@ class Logger extends Utility implements LoggerInterface {
1236
1250
* the event passed to the Lambda function handler should be logged or not.
1237
1251
*/
1238
1252
private setLogEvent ( ) : void {
1239
- if ( this . getEnvVarsService ( ) . getLogEvent ( ) ) {
1240
- this . logEvent = true ;
1241
- }
1253
+ this . logEvent = getBooleanFromEnv ( {
1254
+ key : 'POWERTOOLS_LOGGER_LOG_EVENT' ,
1255
+ defaultValue : false ,
1256
+ } ) ;
1242
1257
}
1243
1258
1244
1259
/**
@@ -1255,7 +1270,6 @@ class Logger extends Utility implements LoggerInterface {
1255
1270
this . logFormatter =
1256
1271
logFormatter ??
1257
1272
new PowertoolsLogFormatter ( {
1258
- envVarsService : this . getEnvVarsService ( ) ,
1259
1273
logRecordOrder,
1260
1274
} ) ;
1261
1275
}
@@ -1265,7 +1279,7 @@ class Logger extends Utility implements LoggerInterface {
1265
1279
* add JSON indentation for pretty printing logs.
1266
1280
*/
1267
1281
private setLogIndentation ( ) : void {
1268
- if ( this . getEnvVarsService ( ) . isDevMode ( ) ) {
1282
+ if ( isDevMode ( ) ) {
1269
1283
this . logIndentation = LogJsonIndent . PRETTY ;
1270
1284
}
1271
1285
}
@@ -1307,7 +1321,13 @@ class Logger extends Utility implements LoggerInterface {
1307
1321
) ;
1308
1322
1309
1323
// configurations that affect Logger behavior
1310
- const AlcLogLevel = this . getEnvVarsService ( ) . getAwsLogLevel ( ) ;
1324
+ const lambdaLogLevel = getStringFromEnv ( {
1325
+ key : 'AWS_LAMBDA_LOG_LEVEL' ,
1326
+ defaultValue : '' ,
1327
+ } ) ;
1328
+ const AlcLogLevel =
1329
+ lambdaLogLevel === 'FATAL' ? 'CRITICAL' : lambdaLogLevel ;
1330
+
1311
1331
if ( this . isValidLogLevel ( AlcLogLevel ) ) {
1312
1332
this . #alcLogLevel = AlcLogLevel ;
1313
1333
}
@@ -1340,15 +1360,23 @@ class Logger extends Utility implements LoggerInterface {
1340
1360
persistentKeys ?: ConstructorOptions [ 'persistentKeys' ]
1341
1361
) : void {
1342
1362
this . addToPowertoolsLogData ( {
1343
- awsRegion : this . getEnvVarsService ( ) . getAwsRegion ( ) ,
1363
+ awsRegion : getStringFromEnv ( {
1364
+ key : 'AWS_REGION' ,
1365
+ } ) ,
1344
1366
environment :
1345
1367
environment ||
1346
1368
this . getCustomConfigService ( ) ?. getCurrentEnvironment ( ) ||
1347
- this . getEnvVarsService ( ) . getCurrentEnvironment ( ) ,
1369
+ getStringFromEnv ( {
1370
+ key : 'ENVIRONMENT' ,
1371
+ defaultValue : '' ,
1372
+ } ) ,
1348
1373
serviceName :
1349
1374
serviceName ||
1350
1375
this . getCustomConfigService ( ) ?. getServiceName ( ) ||
1351
- this . getEnvVarsService ( ) . getServiceName ( ) ||
1376
+ getStringFromEnv ( {
1377
+ key : 'POWERTOOLS_SERVICE_NAME' ,
1378
+ defaultValue : '' ,
1379
+ } ) ||
1352
1380
this . defaultServiceName ,
1353
1381
} ) ;
1354
1382
persistentKeys && this . appendPersistentKeys ( persistentKeys ) ;
@@ -1433,7 +1461,7 @@ class Logger extends Utility implements LoggerInterface {
1433
1461
* your function throws an error.
1434
1462
*/
1435
1463
public flushBuffer ( ) : void {
1436
- const traceId = this . envVarsService . getXrayTraceId ( ) ;
1464
+ const traceId = getXRayTraceIdFromEnv ( ) ;
1437
1465
if ( traceId === undefined ) {
1438
1466
return ;
1439
1467
}
@@ -1477,7 +1505,7 @@ class Logger extends Utility implements LoggerInterface {
1477
1505
* Empties the buffer for the current request
1478
1506
*/
1479
1507
public clearBuffer ( ) : void {
1480
- const traceId = this . envVarsService . getXrayTraceId ( ) ;
1508
+ const traceId = getXRayTraceIdFromEnv ( ) ;
1481
1509
if ( traceId === undefined ) {
1482
1510
return ;
1483
1511
}
0 commit comments