Skip to content
Merged
90 changes: 59 additions & 31 deletions packages/logger/src/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ import type {
HandlerMethodDecorator,
SyncHandler,
} from '@aws-lambda-powertools/commons/types';
import {
getBooleanFromEnv,
getNumberFromEnv,
getStringFromEnv,
getXRayTraceIdFromEnv,
isDevMode,
} from '@aws-lambda-powertools/commons/utils/env';
import type { Context, Handler } from 'aws-lambda';
import merge from 'lodash.merge';
import { EnvironmentVariablesService } from './config/EnvironmentVariablesService.js';
import {
LogJsonIndent,
LogLevelThreshold,
Expand Down Expand Up @@ -112,10 +118,6 @@ class Logger extends Utility implements LoggerInterface {
* Custom config service instance used to configure the logger.
*/
private customConfigService?: ConfigServiceInterface;
/**
* Environment variables service instance used to fetch environment variables.
*/
private envVarsService = new EnvironmentVariablesService();
/**
* Whether to print the Lambda invocation event in the logs.
*/
Expand Down Expand Up @@ -850,7 +852,7 @@ class Logger extends Utility implements LoggerInterface {
const unformattedBaseAttributes = {
logLevel: this.getLogLevelNameFromNumber(logLevel),
timestamp: new Date(),
xRayTraceId: this.envVarsService.getXrayTraceId(),
xRayTraceId: getXRayTraceIdFromEnv(),
...this.getPowertoolsLogData(),
message: '',
};
Expand Down Expand Up @@ -975,13 +977,6 @@ class Logger extends Utility implements LoggerInterface {
return this.customConfigService;
}

/**
* Get the instance of a service that fetches environment variables.
*/
private getEnvVarsService(): EnvironmentVariablesService {
return this.envVarsService as EnvironmentVariablesService;
}

/**
* Get the instance of a service that formats the structure of a
* log item's keys and values in the desired way.
Expand Down Expand Up @@ -1081,7 +1076,7 @@ class Logger extends Utility implements LoggerInterface {
input: LogItemMessage,
extraInput: LogItemExtraInput
): void {
const traceId = this.envVarsService.getXrayTraceId();
const traceId = getXRayTraceIdFromEnv();
if (traceId !== undefined && this.shouldBufferLog(traceId, logLevel)) {
try {
this.bufferLogItem(
Expand Down Expand Up @@ -1125,7 +1120,7 @@ class Logger extends Utility implements LoggerInterface {
* or as the global node console if the `POWERTOOLS_DEV' env variable is set and has truthy value.
*/
private setConsole(): void {
if (!this.getEnvVarsService().isDevMode()) {
if (!isDevMode()) {
this.console = new Console({
stdout: process.stdout,
stderr: process.stderr,
Expand Down Expand Up @@ -1190,9 +1185,21 @@ class Logger extends Utility implements LoggerInterface {

return;
}
const envVarsValue = this.getEnvVarsService()?.getLogLevel()?.toUpperCase();
if (this.isValidLogLevel(envVarsValue)) {
this.logLevel = LogLevelThreshold[envVarsValue];

const logLevelVariable = getStringFromEnv({
key: 'POWERTOOLS_LOG_LEVEL',
defaultValue: '',
});
const logLevelVariableAlias = getStringFromEnv({
key: 'LOG_LEVEL',
defaultValue: '',
});

const logLevelValue =
logLevelVariable !== '' ? logLevelVariable : logLevelVariableAlias;

if (this.isValidLogLevel(logLevelValue)) {
this.logLevel = LogLevelThreshold[logLevelValue];
this.#initialLogLevel = this.logLevel;

return;
Expand All @@ -1212,8 +1219,15 @@ class Logger extends Utility implements LoggerInterface {
const constructorValue = sampleRateValue;
const customConfigValue =
this.getCustomConfigService()?.getSampleRateValue();
const envVarsValue = this.getEnvVarsService().getSampleRateValue();
for (const value of [constructorValue, customConfigValue, envVarsValue]) {
const sampleRateEnvVariable = getNumberFromEnv({
key: 'POWERTOOLS_LOGGER_SAMPLE_RATE',
defaultValue: 0,
});
for (const value of [
constructorValue,
customConfigValue,
sampleRateEnvVariable,
]) {
if (this.isValidSampleRate(value)) {
this.#debugLogSampling.sampleRateValue = value;
this.powertoolsLogData.sampleRateValue = value;
Expand All @@ -1236,9 +1250,10 @@ class Logger extends Utility implements LoggerInterface {
* the event passed to the Lambda function handler should be logged or not.
*/
private setLogEvent(): void {
if (this.getEnvVarsService().getLogEvent()) {
this.logEvent = true;
}
this.logEvent = getBooleanFromEnv({
key: 'POWERTOOLS_LOGGER_LOG_EVENT',
defaultValue: false,
});
}

/**
Expand All @@ -1255,7 +1270,6 @@ class Logger extends Utility implements LoggerInterface {
this.logFormatter =
logFormatter ??
new PowertoolsLogFormatter({
envVarsService: this.getEnvVarsService(),
logRecordOrder,
});
}
Expand All @@ -1265,7 +1279,7 @@ class Logger extends Utility implements LoggerInterface {
* add JSON indentation for pretty printing logs.
*/
private setLogIndentation(): void {
if (this.getEnvVarsService().isDevMode()) {
if (isDevMode()) {
this.logIndentation = LogJsonIndent.PRETTY;
}
}
Expand Down Expand Up @@ -1307,7 +1321,13 @@ class Logger extends Utility implements LoggerInterface {
);

// configurations that affect Logger behavior
const AlcLogLevel = this.getEnvVarsService().getAwsLogLevel();
const lambdaLogLevel = getStringFromEnv({
key: 'AWS_LAMBDA_LOG_LEVEL',
defaultValue: '',
});
const AlcLogLevel =
lambdaLogLevel === 'FATAL' ? 'CRITICAL' : lambdaLogLevel;

if (this.isValidLogLevel(AlcLogLevel)) {
this.#alcLogLevel = AlcLogLevel;
}
Expand Down Expand Up @@ -1340,15 +1360,23 @@ class Logger extends Utility implements LoggerInterface {
persistentKeys?: ConstructorOptions['persistentKeys']
): void {
this.addToPowertoolsLogData({
awsRegion: this.getEnvVarsService().getAwsRegion(),
awsRegion: getStringFromEnv({
key: 'AWS_REGION',
}),
environment:
environment ||
this.getCustomConfigService()?.getCurrentEnvironment() ||
this.getEnvVarsService().getCurrentEnvironment(),
getStringFromEnv({
key: 'ENVIRONMENT',
defaultValue: '',
}),
serviceName:
serviceName ||
this.getCustomConfigService()?.getServiceName() ||
this.getEnvVarsService().getServiceName() ||
getStringFromEnv({
key: 'POWERTOOLS_SERVICE_NAME',
defaultValue: '',
}) ||
this.defaultServiceName,
});
persistentKeys && this.appendPersistentKeys(persistentKeys);
Expand Down Expand Up @@ -1433,7 +1461,7 @@ class Logger extends Utility implements LoggerInterface {
* your function throws an error.
*/
public flushBuffer(): void {
const traceId = this.envVarsService.getXrayTraceId();
const traceId = getXRayTraceIdFromEnv();
if (traceId === undefined) {
return;
}
Expand Down Expand Up @@ -1477,7 +1505,7 @@ class Logger extends Utility implements LoggerInterface {
* Empties the buffer for the current request
*/
public clearBuffer(): void {
const traceId = this.envVarsService.getXrayTraceId();
const traceId = getXRayTraceIdFromEnv();
if (traceId === undefined) {
return;
}
Expand Down
126 changes: 0 additions & 126 deletions packages/logger/src/config/EnvironmentVariablesService.ts

This file was deleted.

29 changes: 11 additions & 18 deletions packages/logger/src/formatter/LogFormatter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { EnvironmentVariablesService } from '../config/EnvironmentVariablesService.js';
import type { LogFormatterOptions } from '../types/formatters.js';
import {
getStringFromEnv,
isDevMode,
} from '@aws-lambda-powertools/commons/utils/env';
import type { LogAttributes } from '../types/Logger.js';
import type { UnformattedAttributes } from '../types/logKeys.js';
import type { LogItem } from './LogItem.js';
Expand All @@ -13,15 +15,6 @@ import type { LogItem } from './LogItem.js';
* @abstract
*/
abstract class LogFormatter {
/**
* Instance of the {@link EnvironmentVariablesService} to use for configuration.
*/
protected envVarsService?: EnvironmentVariablesService;

public constructor(options?: LogFormatterOptions) {
this.envVarsService = options?.envVarsService;
}

/**
* Format key-value pairs of log attributes.
*
Expand Down Expand Up @@ -117,9 +110,7 @@ abstract class LogFormatter {
location: this.getCodeLocation(error.stack),
message,
stack:
this.envVarsService?.isDevMode() && typeof stack === 'string'
? stack?.split('\n')
: stack,
isDevMode() && typeof stack === 'string' ? stack?.split('\n') : stack,
cause: cause instanceof Error ? this.formatError(cause) : cause,
};
for (const key in error) {
Expand All @@ -137,9 +128,7 @@ abstract class LogFormatter {
/**
* Format a date into an ISO 8601 string with the configured timezone.
*
* If the log formatter is passed an {@link EnvironmentVariablesService} instance
* during construction, the timezone is read from the `TZ` environment variable, if present.
*
* The timezone is read from the `TZ` environment variable, if present.
* Otherwise, the timezone defaults to ':UTC'.
*
* @param now - The date to format
Expand All @@ -151,7 +140,11 @@ abstract class LogFormatter {
* If a specific timezone is configured and it's not the default `UTC`,
* format the timestamp with the appropriate timezone offset.
**/
const configuredTimezone = this.envVarsService?.getTimezone();
const configuredTimezone = getStringFromEnv({
key: 'TZ',
defaultValue: '',
});

if (configuredTimezone && !configuredTimezone.includes(defaultTimezone))
return this.#generateISOTimestampWithOffset(now, configuredTimezone);

Expand Down
2 changes: 1 addition & 1 deletion packages/logger/src/formatter/PowertoolsLogFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class PowertoolsLogFormatter extends LogFormatter {
#logRecordOrder?: LogRecordOrderKeys;

public constructor(options?: PowertoolsLogFormatterOptions) {
super(options);
super();
this.#logRecordOrder = options?.logRecordOrder;
}

Expand Down
Loading
Loading