Skip to content

Commit 99c33e4

Browse files
committed
test(logger): improve readability of injectLambdaContextBefore & injectLambdaContextAfterOrOnError function behaviour - separation of concerns
1 parent 7d1bd7c commit 99c33e4

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

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

+8-7
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,12 @@ class Logger extends Utility implements ClassThatLogs {
284284

285285
descriptor.value = (event, context, callback) => {
286286

287-
const initialPersistentAttributes = Logger.injectLambdaContextBefore(this, event, context, options);
287+
let initialPersistentAttributes = {};
288+
if (options && options.clearState === true) {
289+
initialPersistentAttributes = { ...this.getPersistentLogAttributes() };
290+
}
291+
292+
Logger.injectLambdaContextBefore(this, event, context, options);
288293

289294
/* eslint-disable @typescript-eslint/no-non-null-assertion */
290295
let result: unknown;
@@ -301,24 +306,20 @@ class Logger extends Utility implements ClassThatLogs {
301306
};
302307
}
303308

304-
public static injectLambdaContextAfterOrOnError(logger: Logger, initialPersistentAttributes: LogAttributes = {}, options?: HandlerOptions): void {
309+
public static injectLambdaContextAfterOrOnError(logger: Logger, initialPersistentAttributes: LogAttributes, options?: HandlerOptions): void {
305310
if (options && options.clearState === true) {
306311
logger.setPersistentLogAttributes(initialPersistentAttributes);
307312
}
308313
}
309314

310-
public static injectLambdaContextBefore(logger: Logger, event: unknown, context: Context, options?: HandlerOptions): LogAttributes | undefined {
315+
public static injectLambdaContextBefore(logger: Logger, event: unknown, context: Context, options?: HandlerOptions): void {
311316
logger.addContext(context);
312317

313318
let shouldLogEvent = undefined;
314319
if (options && options.hasOwnProperty('logEvent')) {
315320
shouldLogEvent = options.logEvent;
316321
}
317322
logger.logEventIfEnabled(event, shouldLogEvent);
318-
319-
if (options && options.clearState === true) {
320-
return { ...logger.getPersistentLogAttributes() };
321-
}
322323
}
323324

324325
/**

Diff for: packages/logger/src/middleware/middy.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ import { HandlerOptions, LogAttributes } from '../types';
3030
const injectLambdaContext = (target: Logger | Logger[], options?: HandlerOptions): middy.MiddlewareObj => {
3131

3232
const loggers = target instanceof Array ? target : [target];
33-
const persistentAttributes: (LogAttributes | undefined)[] = [];
33+
const persistentAttributes: LogAttributes[] = [];
3434

3535
const injectLambdaContextBefore = async (request: middy.Request): Promise<void> => {
3636
loggers.forEach((logger: Logger) => {
37-
const initialPersistentAttributes = Logger.injectLambdaContextBefore(logger, request.event, request.context, options);
3837
if (options && options.clearState === true) {
39-
persistentAttributes.push(initialPersistentAttributes);
38+
persistentAttributes.push({ ...logger.getPersistentLogAttributes() });
4039
}
40+
Logger.injectLambdaContextBefore(logger, request.event, request.context, options);
4141
});
4242
};
4343

0 commit comments

Comments
 (0)