Skip to content

Commit 5b7c848

Browse files
committed
feat: adopted Utility class & updated unit tests
1 parent a58e636 commit 5b7c848

File tree

3 files changed

+13
-182
lines changed

3 files changed

+13
-182
lines changed

Diff for: packages/logger/src/Logger.ts

+5-84
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Context } from 'aws-lambda';
2+
import { Utility } from '@aws-lambda-powertools/commons';
23
import { LogFormatterInterface, PowertoolLogFormatter } from './formatter';
34
import { LogItem } from './log';
45
import cloneDeep from 'lodash.clonedeep';
@@ -104,11 +105,7 @@ import type {
104105
* @implements {ClassThatLogs}
105106
* @see https://awslabs.github.io/aws-lambda-powertools-typescript/latest/core/logger/
106107
*/
107-
class Logger implements ClassThatLogs {
108-
109-
private static coldStart?: boolean = undefined;
110-
111-
private static coldStartEvaluated: boolean = false;
108+
class Logger extends Utility implements ClassThatLogs {
112109

113110
private customConfigService?: ConfigServiceInterface;
114111

@@ -141,6 +138,8 @@ class Logger implements ClassThatLogs {
141138
* @param {LoggerOptions} options
142139
*/
143140
public constructor(options: LoggerOptions = {}) {
141+
super();
142+
144143
this.setOptions(options);
145144
}
146145

@@ -152,10 +151,9 @@ class Logger implements ClassThatLogs {
152151
* @returns {void}
153152
*/
154153
public addContext(context: Context): void {
155-
Logger.evaluateColdStartOnce();
156154
const lambdaContext: Partial<LambdaFunctionContext> = {
157155
invokedFunctionArn: context.invokedFunctionArn,
158-
coldStart: Logger.getColdStartValue(),
156+
coldStart: this.getColdStart(),
159157
awsRequestId: context.awsRequestId,
160158
memoryLimitInMB: Number(context.memoryLimitInMB),
161159
functionName: context.functionName,
@@ -220,38 +218,6 @@ class Logger implements ClassThatLogs {
220218
this.processLogItem('ERROR', input, extraInput);
221219
}
222220

223-
/**
224-
* It evaluates whether the current Lambda function invocation has a cold start or not.
225-
*
226-
* @static
227-
* @returns {void}
228-
*/
229-
public static evaluateColdStartOnce(): void {
230-
if (!Logger.getColdStartEvaluatedValue()) {
231-
Logger.evaluateColdStart();
232-
}
233-
}
234-
235-
/**
236-
* It returns a boolean value which is true if the current Lambda function cold start has been already evaluated, false otherwise.
237-
*
238-
* @static
239-
* @returns {boolean}
240-
*/
241-
public static getColdStartEvaluatedValue(): boolean {
242-
return Logger.coldStartEvaluated;
243-
}
244-
245-
/**
246-
* It returns an optional boolean value, true if the current Lambda function invocation has a cold start, false otherwise.
247-
*
248-
* @static
249-
* @returns {boolean | undefined}
250-
*/
251-
public static getColdStartValue(): boolean | undefined {
252-
return Logger.coldStart;
253-
}
254-
255221
/**
256222
* It returns a boolean value, if true all the logs will be printed.
257223
*
@@ -305,30 +271,6 @@ class Logger implements ClassThatLogs {
305271
this.setLogsSampled();
306272
}
307273

308-
/**
309-
* It sets the value of a flag static propriety that tracks whether
310-
* the cold start evaluation already took place.
311-
*
312-
* @param {boolean} value
313-
* @static
314-
* @returns {void}
315-
*/
316-
public static setColdStartEvaluatedValue(value: boolean): void {
317-
Logger.coldStartEvaluated = value;
318-
}
319-
320-
/**
321-
* It sets the value of a flag static propriety that tracks whether
322-
* the current Lambda invocation experienced a cold start.
323-
*
324-
* @static
325-
* @param {boolean | undefined} value
326-
* @returns {void}
327-
*/
328-
public static setColdStartValue(value: boolean | undefined): void {
329-
Logger.coldStart = value;
330-
}
331-
332274
/**
333275
* It sets the user-provided sample rate value.
334276
*
@@ -402,27 +344,6 @@ class Logger implements ClassThatLogs {
402344
return logItem;
403345
}
404346

405-
/**
406-
* It evaluates whether the current Lambda invocation experienced a
407-
* cold start.
408-
*
409-
* @private
410-
* @static
411-
* @returns {void}
412-
*/
413-
private static evaluateColdStart(): void {
414-
const coldStartValue = Logger.getColdStartValue();
415-
if (typeof coldStartValue === 'undefined') {
416-
Logger.setColdStartValue(true);
417-
} else if (coldStartValue) {
418-
Logger.setColdStartValue(false);
419-
} else {
420-
Logger.setColdStartValue(false);
421-
}
422-
423-
Logger.setColdStartEvaluatedValue(true);
424-
}
425-
426347
/**
427348
* It returns the custom config service, an abstraction used to fetch environment variables.
428349
*

Diff for: packages/logger/tests/unit/Logger.test.ts

+5-98
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ const consoleSpy = {
2828
describe('Class: Logger', () => {
2929

3030
beforeEach(() => {
31-
Logger.setColdStartValue(undefined);
32-
Logger.setColdStartEvaluatedValue(false);
3331
consoleSpy['debug'].mockClear();
3432
consoleSpy['info'].mockClear();
3533
consoleSpy['warn'].mockClear();
@@ -464,6 +462,7 @@ describe('Class: Logger', () => {
464462

465463
// Assess
466464
expect(logger).toEqual({
465+
coldStart: false, // This is now false because the `coldStart` attribute has been already accessed once by the `addContext` method
467466
customConfigService: undefined,
468467
envVarsService: expect.any(EnvironmentVariablesService),
469468
logFormatter: expect.any(PowertoolLogFormatter),
@@ -602,79 +601,6 @@ describe('Class: Logger', () => {
602601
});
603602
});
604603

605-
describe('Method: createChild', () => {
606-
607-
test('when called, creates a distinct clone of the original logger instance', () => {
608-
609-
// Prepare
610-
const logger = new Logger();
611-
612-
// Act
613-
const childLogger = logger.createChild({
614-
logLevel: 'ERROR',
615-
});
616-
617-
// Assess
618-
expect(logger).toEqual(expect.objectContaining({
619-
logLevel: 'DEBUG',
620-
}));
621-
expect(childLogger).toBeInstanceOf(Logger);
622-
expect(childLogger).toEqual(expect.objectContaining({
623-
logLevel: 'ERROR',
624-
}));
625-
});
626-
627-
});
628-
629-
describe('Method: evaluateColdStartOnce', () => {
630-
631-
test('when called during the first invocation (cold start), it populates the logger\'s PowertoolLogData object with coldstart set to true', () => {
632-
633-
// Prepare
634-
// This value is undefined at the beginning of the first invocation
635-
Logger.setColdStartValue(undefined);
636-
637-
// Act
638-
Logger.evaluateColdStartOnce();
639-
Logger.evaluateColdStartOnce();
640-
Logger.evaluateColdStartOnce();
641-
642-
// Assess
643-
expect(Logger.getColdStartValue()).toEqual(true);
644-
});
645-
646-
test('when called during the SECOND invocation (warm start), it populates the logger\'s PowertoolLogData object with coldstart set to false', () => {
647-
648-
// Prepare
649-
// This value is set to true at the beginning of the second invocation
650-
Logger.setColdStartValue(true);
651-
652-
// Act
653-
Logger.evaluateColdStartOnce();
654-
Logger.evaluateColdStartOnce();
655-
Logger.evaluateColdStartOnce();
656-
657-
// Assess
658-
expect(Logger.getColdStartValue()).toEqual(false);
659-
});
660-
661-
test('when called during the THIRD invocation (warm start), it populates the logger\'s PowertoolLogData object with coldstart set to false', () => {
662-
663-
// Prepare
664-
// This value is set to false at the beginning of the third invocation
665-
Logger.setColdStartValue(false);
666-
667-
// Act
668-
Logger.evaluateColdStartOnce();
669-
Logger.evaluateColdStartOnce();
670-
Logger.evaluateColdStartOnce();
671-
672-
// Assess
673-
expect(Logger.getColdStartValue()).toEqual(false);
674-
});
675-
676-
});
677-
678604
describe('Method: injectLambdaContext', () => {
679605

680606
beforeEach(() => {
@@ -813,29 +739,6 @@ describe('Class: Logger', () => {
813739

814740
});
815741

816-
describe('Method: setColdStartValue', () => {
817-
818-
test('when called, it sets the value of the static variable coldStart in the same file', async () => {
819-
820-
// Act
821-
Logger.setColdStartValue(undefined);
822-
const undefinedValue = Logger.getColdStartValue();
823-
824-
Logger.setColdStartValue(true);
825-
const trueValue = Logger.getColdStartValue();
826-
827-
Logger.setColdStartValue(false);
828-
const falseValue = Logger.getColdStartValue();
829-
830-
// Assess
831-
expect(undefinedValue).toBe(undefined);
832-
expect(trueValue).toBe(true);
833-
expect(falseValue).toBe(false);
834-
835-
});
836-
837-
});
838-
839742
describe('Method: refreshSampleRateCalculation', () => {
840743

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

894797
expect(parentLogger).toEqual({
798+
coldStart: true,
895799
customConfigService: undefined,
896800
envVarsService: expect.any(EnvironmentVariablesService),
897801
logFormatter: expect.any(PowertoolLogFormatter),
@@ -914,6 +818,7 @@ describe('Class: Logger', () => {
914818
});
915819

916820
expect(childLoggerWithPermanentAttributes).toEqual({
821+
coldStart: true,
917822
customConfigService: undefined,
918823
envVarsService: expect.any(EnvironmentVariablesService),
919824
logFormatter: expect.any(PowertoolLogFormatter),
@@ -938,6 +843,7 @@ describe('Class: Logger', () => {
938843
});
939844

940845
expect(childLoggerWithSampleRateEnabled).toEqual({
846+
coldStart: true,
941847
customConfigService: undefined,
942848
envVarsService: expect.any(EnvironmentVariablesService),
943849
logFormatter: expect.any(PowertoolLogFormatter),
@@ -960,6 +866,7 @@ describe('Class: Logger', () => {
960866
});
961867

962868
expect(childLoggerWithErrorLogLevel).toEqual({
869+
coldStart: true,
963870
customConfigService: undefined,
964871
envVarsService: expect.any(EnvironmentVariablesService),
965872
logFormatter: expect.any(PowertoolLogFormatter),

Diff for: packages/logger/tests/unit/helpers.test.ts

+3
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ describe('Helper: createLogger function', () => {
7373
// Assess
7474
expect(logger).toBeInstanceOf(Logger);
7575
expect(logger).toEqual({
76+
coldStart: true,
7677
customConfigService: expect.any(EnvironmentVariablesService),
7778
envVarsService: expect.any(EnvironmentVariablesService),
7879
logFormatter: expect.any(PowertoolLogFormatter),
@@ -111,6 +112,7 @@ describe('Helper: createLogger function', () => {
111112
// Assess
112113
expect(logger).toBeInstanceOf(Logger);
113114
expect(logger).toEqual({
115+
coldStart: true,
114116
customConfigService: undefined,
115117
envVarsService: expect.any(EnvironmentVariablesService),
116118
logFormatter: expect.any(PowertoolLogFormatter),
@@ -233,6 +235,7 @@ describe('Helper: createLogger function', () => {
233235
// Assess
234236
expect(logger).toBeInstanceOf(Logger);
235237
expect(logger).toEqual({
238+
coldStart: true,
236239
customConfigService: undefined,
237240
envVarsService: expect.any(EnvironmentVariablesService),
238241
logFormatter: expect.any(PowertoolLogFormatter),

0 commit comments

Comments
 (0)