Skip to content

Commit 6a7f122

Browse files
committed
add reserved key filter logic to setPersistentLogAttributes
1 parent c128331 commit 6a7f122

File tree

2 files changed

+42
-15
lines changed

2 files changed

+42
-15
lines changed

packages/logger/src/Logger.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,8 @@ class Logger extends Utility implements LoggerInterface {
656656
* @deprecated This method is deprecated and will be removed in the future major versions, please use {@link appendPersistentKeys() `appendPersistentKeys()`} instead.
657657
*/
658658
public setPersistentLogAttributes(attributes: LogKeys): void {
659-
this.#attributesStore.setPersistentAttributes(attributes);
659+
const filtered = this.#filterReservedAttributeKeys(attributes);
660+
this.#attributesStore.setPersistentAttributes(filtered);
660661
}
661662

662663
/**
@@ -779,19 +780,24 @@ class Logger extends Utility implements LoggerInterface {
779780
merge(this.powertoolsLogData, attributes);
780781
}
781782

783+
#filterReservedAttributeKeys(attributes: LogKeys) {
784+
const filtered: LogKeys = {};
785+
for (const [key, value] of Object.entries(attributes)) {
786+
if (!this.#checkReservedKeyAndWarn(key)) {
787+
filtered[key] = value;
788+
}
789+
}
790+
return filtered;
791+
}
792+
782793
/**
783794
* Shared logic for adding keys to the logger instance.
784795
*
785796
* @param attributes - The attributes to add to the log item.
786797
* @param type - The type of the attributes to add.
787798
*/
788799
#appendKeys(attributes: LogKeys, type: 'temp' | 'persistent'): void {
789-
const filtered: LogKeys = {};
790-
for (const [key, value] of Object.entries(attributes)) {
791-
if (!this.#checkReservedKeyAndWarn(key)) {
792-
filtered[key] = value;
793-
}
794-
}
800+
const filtered = this.#filterReservedAttributeKeys(attributes);
795801
if (type === 'temp') {
796802
this.#attributesStore.appendTemporaryKeys(filtered);
797803
} else {
@@ -834,7 +840,7 @@ class Logger extends Utility implements LoggerInterface {
834840
...this.getPowertoolsLogData(),
835841
message: '',
836842
};
837-
const additionalAttributes = this.#createAdditionalAttributes();
843+
const additionalAttributes = this.#attributesStore.getAllAttributes();
838844

839845
this.#processMainInput(
840846
input,
@@ -849,13 +855,6 @@ class Logger extends Utility implements LoggerInterface {
849855
);
850856
}
851857

852-
/**
853-
* Create additional attributes from persistent and temporary keys
854-
*/
855-
#createAdditionalAttributes(): LogAttributes {
856-
return this.#attributesStore.getAllAttributes();
857-
}
858-
859858
/**
860859
* Process the main input message and add it to the attributes
861860
*/

packages/logger/tests/unit/workingWithkeys.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,34 @@ describe('Working with keys', () => {
619619
);
620620
});
621621

622+
it('does not allow reserved keys with the deprecated setPersistentLogAttributes() method', () => {
623+
// Prepare
624+
const logger = new Logger();
625+
626+
// Act
627+
logger.setPersistentLogAttributes({
628+
// @ts-expect-error - testing invalid key at runtime
629+
level: 'bar',
630+
});
631+
632+
logger.info('test');
633+
634+
expect(console.info).toHaveLoggedNth(
635+
1,
636+
expect.objectContaining({
637+
level: 'INFO',
638+
})
639+
);
640+
641+
// Assess
642+
expect(console.warn).toHaveLoggedNth(
643+
1,
644+
expect.objectContaining({
645+
message: 'The key "level" is a reserved key and will be dropped.',
646+
})
647+
);
648+
});
649+
622650
it('logs a warning when using both the deprecated persistentLogAttributes and persistentKeys options', () => {
623651
// Prepare
624652
new Logger({

0 commit comments

Comments
 (0)