Skip to content

Commit 8b6e2bb

Browse files
committed
feat(logger): middleware, cold start business logic + tests
1 parent 63eb569 commit 8b6e2bb

File tree

4 files changed

+218
-102
lines changed

4 files changed

+218
-102
lines changed

packages/logger/src/Logger.ts

+37-20
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ import { LogItem } from './log';
2020

2121
class Logger implements ClassThatLogs {
2222

23-
public static coldStart?: boolean;
23+
private static coldStart?: boolean = undefined;
24+
25+
private static coldStartEvaluated: boolean = false;
2426

2527
private static readonly defaultLogLevel: LogLevel = 'INFO';
2628

@@ -47,15 +49,31 @@ class Logger implements ClassThatLogs {
4749

4850
private powertoolLogData: PowertoolLogData = <PowertoolLogData>{};
4951

52+
public static getColdStartEvaluatedValue(): boolean {
53+
return Logger.coldStartEvaluated;
54+
}
55+
56+
public static setColdStartEvaluatedValue(value: boolean) {
57+
Logger.coldStartEvaluated = value;
58+
}
59+
60+
public static getColdStartValue(): boolean | undefined {
61+
return Logger.coldStart;
62+
}
63+
64+
public static setColdStartValue(value: boolean | undefined) {
65+
Logger.coldStart = value;
66+
}
67+
5068
public constructor(options: LoggerOptions = {}) {
5169
this.setOptions(options);
5270
}
5371

5472
public addContext(context: Context): void {
55-
Logger.evaluateColdStart();
73+
Logger.evaluateColdStartOnce();
5674
const lambdaContext: Partial<LambdaFunctionContext> = {
5775
invokedFunctionArn: context.invokedFunctionArn,
58-
coldStart: Logger.coldStart,
76+
coldStart: Logger.getColdStartValue(),
5977
awsRequestId: context.awsRequestId,
6078
memoryLimitInMB: Number(context.memoryLimitInMB),
6179
functionName: context.functionName,
@@ -162,25 +180,24 @@ class Logger implements ClassThatLogs {
162180
return logItem;
163181
}
164182

165-
private static evaluateColdStart = (() => {
166-
let evaluated = false;
167-
return function() {
168-
if (!evaluated) {
169-
evaluated = true;
170-
if(typeof Logger.coldStart === 'undefined') {
171-
Logger.coldStart = true;
172-
return;
173-
}
174-
if(Logger.coldStart === true) {
175-
Logger.coldStart = false;
176-
return;
177-
}
183+
public static evaluateColdStartOnce() {
184+
if (Logger.getColdStartEvaluatedValue() === false) {
185+
Logger.evaluateColdStart();
186+
}
187+
}
178188

179-
Logger.coldStart = false;
189+
protected static evaluateColdStart() {
190+
const coldStartValue = Logger.getColdStartValue();
191+
if(typeof coldStartValue === 'undefined') {
192+
Logger.setColdStartValue(true);
193+
} else if(coldStartValue === true) {
194+
Logger.setColdStartValue(false);
195+
} else {
196+
Logger.setColdStartValue(false);
197+
}
180198

181-
}
182-
};
183-
})()
199+
Logger.setColdStartEvaluatedValue(true);
200+
}
184201

185202
private getCustomConfigService(): ConfigServiceInterface | undefined {
186203
return this.customConfigService;

packages/logger/src/middleware/middy.ts

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ const injectLambdaContext = (target: Logger | Logger[]): middy.MiddlewareObj =>
66
const loggers = target instanceof Array ? target : [ target ];
77
loggers.forEach((logger: Logger) => {
88
logger.addContext(request.context);
9-
console.log('foo', logger);
109
})
1110
};
1211
return {

0 commit comments

Comments
 (0)