Skip to content

Commit dd404c0

Browse files
committed
add requested changes
1 parent 25deb2a commit dd404c0

File tree

2 files changed

+31
-43
lines changed

2 files changed

+31
-43
lines changed

packages/logger/src/Logger.ts

Lines changed: 30 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -172,16 +172,16 @@ class Logger extends Utility implements LoggerInterface {
172172
* Standard attributes managed by Powertools that will be logged in all log items.
173173
*/
174174
private powertoolsLogData: PowertoolsLogData = <PowertoolsLogData>{};
175+
/**
176+
* Temporary log attributes that can be appended with `appendKeys()` method.
177+
*/
178+
private temporaryLogAttributes: LogAttributes = {};
175179
/**
176180
* Buffer used to store logs until the logger is initialized.
177181
*
178182
* Sometimes we need to log warnings before the logger is fully initialized, however we can't log them
179183
* immediately because the logger is not ready yet. This buffer stores those logs until the logger is ready.
180184
*/
181-
/**
182-
* Temporary log attributes that can be appended with `appendKeys()` method.
183-
*/
184-
private temporaryLogAttributes: LogAttributes = {};
185185
#buffer: [number, Parameters<Logger['createAndPopulateLogItem']>][] = [];
186186
/**
187187
* Flag used to determine if the logger is initialized.
@@ -257,15 +257,6 @@ class Logger extends Utility implements LoggerInterface {
257257
merge(this.temporaryLogAttributes, attributes);
258258
}
259259

260-
/**
261-
* It clears temporary log attributes.
262-
*
263-
* @returns {void}
264-
*/
265-
public clearState(): void {
266-
this.temporaryLogAttributes = {};
267-
}
268-
269260
/**
270261
* It creates a separate Logger instance, identical to the current one
271262
* It's possible to overwrite the new instance options by passing them.
@@ -368,16 +359,6 @@ class Logger extends Utility implements LoggerInterface {
368359
return this.persistentLogAttributes;
369360
}
370361

371-
/**
372-
* It returns the temporary log attributes, added with `appendKeys()` method.
373-
*
374-
* @private
375-
* @returns {LogAttributes}
376-
*/
377-
public getTemporaryLogAttributes(): LogAttributes {
378-
return this.temporaryLogAttributes;
379-
}
380-
381362
/**
382363
* It prints a log item with level INFO.
383364
*
@@ -449,26 +430,24 @@ class Logger extends Utility implements LoggerInterface {
449430
} catch (error) {
450431
throw error;
451432
} finally {
452-
Logger.injectLambdaContextAfterOrOnError(
453-
loggerRef,
454-
loggerRef.getPersistentLogAttributes(),
455-
options
456-
);
433+
Logger.injectLambdaContextAfterOrOnError(loggerRef, {}, options);
457434
}
458435

459436
return result;
460437
};
461438
};
462439
}
463440

441+
/**
442+
* @deprecated This method is deprecated and will be removed in the future major versions.
443+
*/
464444
public static injectLambdaContextAfterOrOnError(
465445
logger: Logger,
466-
persistentAttributes: LogAttributes,
446+
_persistentAttributes: LogAttributes,
467447
options?: InjectLambdaContextOptions
468448
): void {
469449
if (options && options.clearState === true) {
470-
logger.clearState();
471-
logger.addPersistentLogAttributes(persistentAttributes);
450+
logger.resetState();
472451
}
473452
}
474453

@@ -511,31 +490,40 @@ class Logger extends Utility implements LoggerInterface {
511490
}
512491

513492
/**
514-
* It removes attributes based on provided keys to all log items generated by this Logger instance.
493+
* It removes temporary attributes based on provided keys to all log items generated by this Logger instance.
515494
*
516495
* @param {string[]} keys
517496
* @returns {void}
518497
*/
519498
public removeKeys(keys: string[]): void {
520-
const removeKey = (attributes: LogAttributes, key: string): void => {
521-
if (attributes && key in attributes) {
522-
delete attributes[key];
523-
}
524-
};
525499
for (const key of keys) {
526-
removeKey(this.persistentLogAttributes, key);
527-
removeKey(this.temporaryLogAttributes, key);
500+
if (this.temporaryLogAttributes && key in this.temporaryLogAttributes) {
501+
delete this.temporaryLogAttributes[key];
502+
}
528503
}
529504
}
530505

531506
/**
532-
* Alias for removeKeys().
507+
* It removes persistent attributes based on provided keys to all log items generated by this Logger instance.
533508
*
534509
* @param {string[]} keys
535510
* @returns {void}
536511
*/
537512
public removePersistentLogAttributes(keys: string[]): void {
538-
this.removeKeys(keys);
513+
for (const key of keys) {
514+
if (this.persistentLogAttributes && key in this.persistentLogAttributes) {
515+
delete this.persistentLogAttributes[key];
516+
}
517+
}
518+
}
519+
520+
/**
521+
* It resets the state, by removing all temporary log attributes added with `appendKeys()` method.
522+
*
523+
* @returns {void}
524+
*/
525+
public resetState(): void {
526+
this.temporaryLogAttributes = {};
539527
}
540528

541529
/**
@@ -692,7 +680,7 @@ class Logger extends Utility implements LoggerInterface {
692680
// gradually merge additional attributes starting from customer-provided persistent & temporary attributes
693681
let additionalLogAttributes = {
694682
...this.getPersistentLogAttributes(),
695-
...this.getTemporaryLogAttributes(),
683+
...this.temporaryLogAttributes,
696684
};
697685
// if the main input is not a string, then it's an object with additional attributes, so we merge it
698686
additionalLogAttributes = merge(additionalLogAttributes, otherInput);

packages/logger/src/middleware/middy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ const injectLambdaContext = (
6969
const injectLambdaContextAfterOrOnError = async (): Promise<void> => {
7070
if (isClearState) {
7171
loggers.forEach((logger: Logger) => {
72-
logger.clearState();
72+
logger.resetState();
7373
});
7474
}
7575
};

0 commit comments

Comments
 (0)