Skip to content

Commit b30c9c4

Browse files
authored
fix(logger): merged temp keys with same keys when appending to the logger (#4840)
1 parent 919c26d commit b30c9c4

File tree

2 files changed

+81
-21
lines changed

2 files changed

+81
-21
lines changed

packages/logger/src/LogAttributesStore.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import '@aws/lambda-invoke-store';
22
import { shouldUseInvokeStore } from '@aws-lambda-powertools/commons/utils/env';
3+
import merge from 'lodash.merge';
34
import type { LogAttributes } from './types/logKeys.js';
45

56
/**
@@ -62,11 +63,11 @@ class LogAttributesStore {
6263

6364
public appendTemporaryKeys(attributes: LogAttributes): void {
6465
const tempAttrs = this.#getTemporaryAttributes();
65-
const keys = this.#getKeys();
66+
merge(tempAttrs, attributes);
6667

67-
for (const [key, value] of Object.entries(attributes)) {
68-
tempAttrs[key] = value;
69-
keys.set(key, 'temp');
68+
const keysMap = this.#getKeys();
69+
for (const key of Object.keys(attributes)) {
70+
keysMap.set(key, 'temp');
7071
}
7172
}
7273

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

Lines changed: 76 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,38 @@ describe('Working with keys', () => {
185185
);
186186
});
187187

188+
it('merges temporary keys with object values when the same key is added', () => {
189+
// Prepare
190+
const logger = new Logger();
191+
192+
// Act
193+
logger.appendKeys({
194+
foo: { key1: 'bar' },
195+
});
196+
logger.info('Hello, world!');
197+
logger.appendKeys({
198+
foo: { key2: 'baz' },
199+
});
200+
logger.info('Hello, world!');
201+
202+
// Assess
203+
expect(console.info).toHaveBeenCalledTimes(2);
204+
expect(console.info).toHaveLoggedNth(
205+
1,
206+
expect.objectContaining({
207+
message: 'Hello, world!',
208+
foo: { key1: 'bar' },
209+
})
210+
);
211+
expect(console.info).toHaveLoggedNth(
212+
2,
213+
expect.objectContaining({
214+
message: 'Hello, world!',
215+
foo: { key1: 'bar', key2: 'baz' },
216+
})
217+
);
218+
});
219+
188220
it('adds the temporary keys and clears them when calling resetKeys()', () => {
189221
// Prepare
190222
const logger = new Logger();
@@ -283,6 +315,31 @@ describe('Working with keys', () => {
283315
);
284316
});
285317

318+
it('merges persistent keys with object values when the same key is added', () => {
319+
// Prepare
320+
const logger = new Logger({
321+
persistentKeys: {
322+
foo: { key1: 'bar' },
323+
},
324+
});
325+
326+
// Act
327+
logger.appendPersistentKeys({
328+
foo: { key2: 'baz' },
329+
});
330+
logger.info('Hello, world!');
331+
332+
// Assess
333+
expect(console.info).toHaveBeenCalledTimes(1);
334+
expect(console.info).toHaveLoggedNth(
335+
1,
336+
expect.objectContaining({
337+
message: 'Hello, world!',
338+
foo: { key1: 'bar', key2: 'baz' },
339+
})
340+
);
341+
});
342+
286343
it('overrides temporary keys when the same key is added as persistent key', () => {
287344
// Prepare
288345
const logger = new Logger();
@@ -806,25 +863,27 @@ describe('Working with keys', () => {
806863
);
807864
});
808865

809-
it.each([{ value: null }, { value: undefined }])(
810-
'handles null and undefined values when passing them to the log method ($value)',
811-
({ value }) => {
812-
// Prepare
813-
const logger = new Logger();
866+
it.each([
867+
{ value: null },
868+
{ value: undefined },
869+
])('handles null and undefined values when passing them to the log method ($value)', ({
870+
value,
871+
}) => {
872+
// Prepare
873+
const logger = new Logger();
814874

815-
// Act
816-
// @ts-expect-error - these values are already forbidden by TypeScript, but JavaScript-only customers might pass them
817-
logger.info('foo', value);
875+
// Act
876+
// @ts-expect-error - these values are already forbidden by TypeScript, but JavaScript-only customers might pass them
877+
logger.info('foo', value);
818878

819-
// Assess
820-
expect(console.info).toHaveLoggedNth(
821-
1,
822-
expect.objectContaining({
823-
message: 'foo',
824-
})
825-
);
826-
}
827-
);
879+
// Assess
880+
expect(console.info).toHaveLoggedNth(
881+
1,
882+
expect.objectContaining({
883+
message: 'foo',
884+
})
885+
);
886+
});
828887

829888
describe('deprecated persistentLogAttributes usage', () => {
830889
it('sets #attributesStore on the logger', () => {

0 commit comments

Comments
 (0)