Skip to content

Commit d4b93be

Browse files
committed
feat: adopted Utility class & updated unit tests
1 parent 313596c commit d4b93be

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();
@@ -462,6 +460,7 @@ describe('Class: Logger', () => {
462460

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

528-
describe('Method: createChild', () => {
529-
530-
test('when called, creates a distinct clone of the original logger instance', () => {
531-
532-
// Prepare
533-
const logger = new Logger();
534-
535-
// Act
536-
const childLogger = logger.createChild({
537-
logLevel: 'ERROR',
538-
});
539-
540-
// Assess
541-
expect(logger).toEqual(expect.objectContaining({
542-
logLevel: 'DEBUG',
543-
}));
544-
expect(childLogger).toBeInstanceOf(Logger);
545-
expect(childLogger).toEqual(expect.objectContaining({
546-
logLevel: 'ERROR',
547-
}));
548-
});
549-
550-
});
551-
552-
describe('Method: evaluateColdStartOnce', () => {
553-
554-
test('when called during the first invocation (cold start), it populates the logger\'s PowertoolLogData object with coldstart set to true', () => {
555-
556-
// Prepare
557-
// This value is undefined at the beginning of the first invocation
558-
Logger.setColdStartValue(undefined);
559-
560-
// Act
561-
Logger.evaluateColdStartOnce();
562-
Logger.evaluateColdStartOnce();
563-
Logger.evaluateColdStartOnce();
564-
565-
// Assess
566-
expect(Logger.getColdStartValue()).toEqual(true);
567-
});
568-
569-
test('when called during the SECOND invocation (warm start), it populates the logger\'s PowertoolLogData object with coldstart set to false', () => {
570-
571-
// Prepare
572-
// This value is set to true at the beginning of the second invocation
573-
Logger.setColdStartValue(true);
574-
575-
// Act
576-
Logger.evaluateColdStartOnce();
577-
Logger.evaluateColdStartOnce();
578-
Logger.evaluateColdStartOnce();
579-
580-
// Assess
581-
expect(Logger.getColdStartValue()).toEqual(false);
582-
});
583-
584-
test('when called during the THIRD invocation (warm start), it populates the logger\'s PowertoolLogData object with coldstart set to false', () => {
585-
586-
// Prepare
587-
// This value is set to false at the beginning of the third invocation
588-
Logger.setColdStartValue(false);
589-
590-
// Act
591-
Logger.evaluateColdStartOnce();
592-
Logger.evaluateColdStartOnce();
593-
Logger.evaluateColdStartOnce();
594-
595-
// Assess
596-
expect(Logger.getColdStartValue()).toEqual(false);
597-
});
598-
599-
});
600-
601527
describe('Method: injectLambdaContext', () => {
602528

603529
beforeEach(() => {
@@ -736,29 +662,6 @@ describe('Class: Logger', () => {
736662

737663
});
738664

739-
describe('Method: setColdStartValue', () => {
740-
741-
test('when called, it sets the value of the static variable coldStart in the same file', async () => {
742-
743-
// Act
744-
Logger.setColdStartValue(undefined);
745-
const undefinedValue = Logger.getColdStartValue();
746-
747-
Logger.setColdStartValue(true);
748-
const trueValue = Logger.getColdStartValue();
749-
750-
Logger.setColdStartValue(false);
751-
const falseValue = Logger.getColdStartValue();
752-
753-
// Assess
754-
expect(undefinedValue).toBe(undefined);
755-
expect(trueValue).toBe(true);
756-
expect(falseValue).toBe(false);
757-
758-
});
759-
760-
});
761-
762665
describe('Method: refreshSampleRateCalculation', () => {
763666

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

817720
expect(parentLogger).toEqual({
721+
coldStart: true,
818722
customConfigService: undefined,
819723
envVarsService: expect.any(EnvironmentVariablesService),
820724
logFormatter: expect.any(PowertoolLogFormatter),
@@ -837,6 +741,7 @@ describe('Class: Logger', () => {
837741
});
838742

839743
expect(childLoggerWithPermanentAttributes).toEqual({
744+
coldStart: true,
840745
customConfigService: undefined,
841746
envVarsService: expect.any(EnvironmentVariablesService),
842747
logFormatter: expect.any(PowertoolLogFormatter),
@@ -861,6 +766,7 @@ describe('Class: Logger', () => {
861766
});
862767

863768
expect(childLoggerWithSampleRateEnabled).toEqual({
769+
coldStart: true,
864770
customConfigService: undefined,
865771
envVarsService: expect.any(EnvironmentVariablesService),
866772
logFormatter: expect.any(PowertoolLogFormatter),
@@ -883,6 +789,7 @@ describe('Class: Logger', () => {
883789
});
884790

885791
expect(childLoggerWithErrorLogLevel).toEqual({
792+
coldStart: true,
886793
customConfigService: undefined,
887794
envVarsService: expect.any(EnvironmentVariablesService),
888795
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)