Skip to content

Commit

Permalink
feat: adopted Utility class & updated unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamorosi committed Feb 10, 2022
1 parent 313596c commit d4b93be
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 182 deletions.
89 changes: 5 additions & 84 deletions packages/logger/src/Logger.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Context } from 'aws-lambda';
import { Utility } from '@aws-lambda-powertools/commons';
import { LogFormatterInterface, PowertoolLogFormatter } from './formatter';
import { LogItem } from './log';
import cloneDeep from 'lodash.clonedeep';
Expand Down Expand Up @@ -104,11 +105,7 @@ import type {
* @implements {ClassThatLogs}
* @see https://awslabs.github.io/aws-lambda-powertools-typescript/latest/core/logger/
*/
class Logger implements ClassThatLogs {

private static coldStart?: boolean = undefined;

private static coldStartEvaluated: boolean = false;
class Logger extends Utility implements ClassThatLogs {

private customConfigService?: ConfigServiceInterface;

Expand Down Expand Up @@ -141,6 +138,8 @@ class Logger implements ClassThatLogs {
* @param {LoggerOptions} options
*/
public constructor(options: LoggerOptions = {}) {
super();

this.setOptions(options);
}

Expand All @@ -152,10 +151,9 @@ class Logger implements ClassThatLogs {
* @returns {void}
*/
public addContext(context: Context): void {
Logger.evaluateColdStartOnce();
const lambdaContext: Partial<LambdaFunctionContext> = {
invokedFunctionArn: context.invokedFunctionArn,
coldStart: Logger.getColdStartValue(),
coldStart: this.getColdStart(),
awsRequestId: context.awsRequestId,
memoryLimitInMB: Number(context.memoryLimitInMB),
functionName: context.functionName,
Expand Down Expand Up @@ -220,38 +218,6 @@ class Logger implements ClassThatLogs {
this.processLogItem('ERROR', input, extraInput);
}

/**
* It evaluates whether the current Lambda function invocation has a cold start or not.
*
* @static
* @returns {void}
*/
public static evaluateColdStartOnce(): void {
if (!Logger.getColdStartEvaluatedValue()) {
Logger.evaluateColdStart();
}
}

/**
* It returns a boolean value which is true if the current Lambda function cold start has been already evaluated, false otherwise.
*
* @static
* @returns {boolean}
*/
public static getColdStartEvaluatedValue(): boolean {
return Logger.coldStartEvaluated;
}

/**
* It returns an optional boolean value, true if the current Lambda function invocation has a cold start, false otherwise.
*
* @static
* @returns {boolean | undefined}
*/
public static getColdStartValue(): boolean | undefined {
return Logger.coldStart;
}

/**
* It returns a boolean value, if true all the logs will be printed.
*
Expand Down Expand Up @@ -305,30 +271,6 @@ class Logger implements ClassThatLogs {
this.setLogsSampled();
}

/**
* It sets the value of a flag static propriety that tracks whether
* the cold start evaluation already took place.
*
* @param {boolean} value
* @static
* @returns {void}
*/
public static setColdStartEvaluatedValue(value: boolean): void {
Logger.coldStartEvaluated = value;
}

/**
* It sets the value of a flag static propriety that tracks whether
* the current Lambda invocation experienced a cold start.
*
* @static
* @param {boolean | undefined} value
* @returns {void}
*/
public static setColdStartValue(value: boolean | undefined): void {
Logger.coldStart = value;
}

/**
* It sets the user-provided sample rate value.
*
Expand Down Expand Up @@ -402,27 +344,6 @@ class Logger implements ClassThatLogs {
return logItem;
}

/**
* It evaluates whether the current Lambda invocation experienced a
* cold start.
*
* @private
* @static
* @returns {void}
*/
private static evaluateColdStart(): void {
const coldStartValue = Logger.getColdStartValue();
if (typeof coldStartValue === 'undefined') {
Logger.setColdStartValue(true);
} else if (coldStartValue) {
Logger.setColdStartValue(false);
} else {
Logger.setColdStartValue(false);
}

Logger.setColdStartEvaluatedValue(true);
}

/**
* It returns the custom config service, an abstraction used to fetch environment variables.
*
Expand Down
103 changes: 5 additions & 98 deletions packages/logger/tests/unit/Logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ const consoleSpy = {
describe('Class: Logger', () => {

beforeEach(() => {
Logger.setColdStartValue(undefined);
Logger.setColdStartEvaluatedValue(false);
consoleSpy['debug'].mockClear();
consoleSpy['info'].mockClear();
consoleSpy['warn'].mockClear();
Expand Down Expand Up @@ -462,6 +460,7 @@ describe('Class: Logger', () => {

// Assess
expect(logger).toEqual({
coldStart: false, // This is now false because the `coldStart` attribute has been already accessed once by the `addContext` method
customConfigService: undefined,
envVarsService: expect.any(EnvironmentVariablesService),
logFormatter: expect.any(PowertoolLogFormatter),
Expand Down Expand Up @@ -525,79 +524,6 @@ describe('Class: Logger', () => {
});
});

describe('Method: createChild', () => {

test('when called, creates a distinct clone of the original logger instance', () => {

// Prepare
const logger = new Logger();

// Act
const childLogger = logger.createChild({
logLevel: 'ERROR',
});

// Assess
expect(logger).toEqual(expect.objectContaining({
logLevel: 'DEBUG',
}));
expect(childLogger).toBeInstanceOf(Logger);
expect(childLogger).toEqual(expect.objectContaining({
logLevel: 'ERROR',
}));
});

});

describe('Method: evaluateColdStartOnce', () => {

test('when called during the first invocation (cold start), it populates the logger\'s PowertoolLogData object with coldstart set to true', () => {

// Prepare
// This value is undefined at the beginning of the first invocation
Logger.setColdStartValue(undefined);

// Act
Logger.evaluateColdStartOnce();
Logger.evaluateColdStartOnce();
Logger.evaluateColdStartOnce();

// Assess
expect(Logger.getColdStartValue()).toEqual(true);
});

test('when called during the SECOND invocation (warm start), it populates the logger\'s PowertoolLogData object with coldstart set to false', () => {

// Prepare
// This value is set to true at the beginning of the second invocation
Logger.setColdStartValue(true);

// Act
Logger.evaluateColdStartOnce();
Logger.evaluateColdStartOnce();
Logger.evaluateColdStartOnce();

// Assess
expect(Logger.getColdStartValue()).toEqual(false);
});

test('when called during the THIRD invocation (warm start), it populates the logger\'s PowertoolLogData object with coldstart set to false', () => {

// Prepare
// This value is set to false at the beginning of the third invocation
Logger.setColdStartValue(false);

// Act
Logger.evaluateColdStartOnce();
Logger.evaluateColdStartOnce();
Logger.evaluateColdStartOnce();

// Assess
expect(Logger.getColdStartValue()).toEqual(false);
});

});

describe('Method: injectLambdaContext', () => {

beforeEach(() => {
Expand Down Expand Up @@ -736,29 +662,6 @@ describe('Class: Logger', () => {

});

describe('Method: setColdStartValue', () => {

test('when called, it sets the value of the static variable coldStart in the same file', async () => {

// Act
Logger.setColdStartValue(undefined);
const undefinedValue = Logger.getColdStartValue();

Logger.setColdStartValue(true);
const trueValue = Logger.getColdStartValue();

Logger.setColdStartValue(false);
const falseValue = Logger.getColdStartValue();

// Assess
expect(undefinedValue).toBe(undefined);
expect(trueValue).toBe(true);
expect(falseValue).toBe(false);

});

});

describe('Method: refreshSampleRateCalculation', () => {

test('when called, it recalculates whether the current Lambda invocation\'s logs will be printed or not', () => {
Expand Down Expand Up @@ -815,6 +718,7 @@ describe('Class: Logger', () => {
expect(parentLogger === childLoggerWithErrorLogLevel).toBe(false);

expect(parentLogger).toEqual({
coldStart: true,
customConfigService: undefined,
envVarsService: expect.any(EnvironmentVariablesService),
logFormatter: expect.any(PowertoolLogFormatter),
Expand All @@ -837,6 +741,7 @@ describe('Class: Logger', () => {
});

expect(childLoggerWithPermanentAttributes).toEqual({
coldStart: true,
customConfigService: undefined,
envVarsService: expect.any(EnvironmentVariablesService),
logFormatter: expect.any(PowertoolLogFormatter),
Expand All @@ -861,6 +766,7 @@ describe('Class: Logger', () => {
});

expect(childLoggerWithSampleRateEnabled).toEqual({
coldStart: true,
customConfigService: undefined,
envVarsService: expect.any(EnvironmentVariablesService),
logFormatter: expect.any(PowertoolLogFormatter),
Expand All @@ -883,6 +789,7 @@ describe('Class: Logger', () => {
});

expect(childLoggerWithErrorLogLevel).toEqual({
coldStart: true,
customConfigService: undefined,
envVarsService: expect.any(EnvironmentVariablesService),
logFormatter: expect.any(PowertoolLogFormatter),
Expand Down
3 changes: 3 additions & 0 deletions packages/logger/tests/unit/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ describe('Helper: createLogger function', () => {
// Assess
expect(logger).toBeInstanceOf(Logger);
expect(logger).toEqual({
coldStart: true,
customConfigService: expect.any(EnvironmentVariablesService),
envVarsService: expect.any(EnvironmentVariablesService),
logFormatter: expect.any(PowertoolLogFormatter),
Expand Down Expand Up @@ -111,6 +112,7 @@ describe('Helper: createLogger function', () => {
// Assess
expect(logger).toBeInstanceOf(Logger);
expect(logger).toEqual({
coldStart: true,
customConfigService: undefined,
envVarsService: expect.any(EnvironmentVariablesService),
logFormatter: expect.any(PowertoolLogFormatter),
Expand Down Expand Up @@ -233,6 +235,7 @@ describe('Helper: createLogger function', () => {
// Assess
expect(logger).toBeInstanceOf(Logger);
expect(logger).toEqual({
coldStart: true,
customConfigService: undefined,
envVarsService: expect.any(EnvironmentVariablesService),
logFormatter: expect.any(PowertoolLogFormatter),
Expand Down

0 comments on commit d4b93be

Please sign in to comment.