Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions packages/logger/src/LogAttributesStore.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import '@aws/lambda-invoke-store';
import { shouldUseInvokeStore } from '@aws-lambda-powertools/commons/utils/env';
import merge from 'lodash.merge';
import type { LogAttributes } from './types/logKeys.js';

/**
Expand Down Expand Up @@ -62,11 +63,11 @@ class LogAttributesStore {

public appendTemporaryKeys(attributes: LogAttributes): void {
const tempAttrs = this.#getTemporaryAttributes();
const keys = this.#getKeys();
merge(tempAttrs, attributes);

for (const [key, value] of Object.entries(attributes)) {
tempAttrs[key] = value;
keys.set(key, 'temp');
const keysMap = this.#getKeys();
for (const key of Object.keys(attributes)) {
keysMap.set(key, 'temp');
}
}

Expand Down
93 changes: 76 additions & 17 deletions packages/logger/tests/unit/workingWithkeys.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,38 @@ describe('Working with keys', () => {
);
});

it('merges temporary keys with object values when the same key is added', () => {
// Prepare
const logger = new Logger();

// Act
logger.appendKeys({
foo: { key1: 'bar' },
});
logger.info('Hello, world!');
logger.appendKeys({
foo: { key2: 'baz' },
});
logger.info('Hello, world!');

// Assess
expect(console.info).toHaveBeenCalledTimes(2);
expect(console.info).toHaveLoggedNth(
1,
expect.objectContaining({
message: 'Hello, world!',
foo: { key1: 'bar' },
})
);
expect(console.info).toHaveLoggedNth(
2,
expect.objectContaining({
message: 'Hello, world!',
foo: { key1: 'bar', key2: 'baz' },
})
);
});

it('adds the temporary keys and clears them when calling resetKeys()', () => {
// Prepare
const logger = new Logger();
Expand Down Expand Up @@ -283,6 +315,31 @@ describe('Working with keys', () => {
);
});

it('merges persistent keys with object values when the same key is added', () => {
// Prepare
const logger = new Logger({
persistentKeys: {
foo: { key1: 'bar' },
},
});

// Act
logger.appendPersistentKeys({
foo: { key2: 'baz' },
});
logger.info('Hello, world!');

// Assess
expect(console.info).toHaveBeenCalledTimes(1);
expect(console.info).toHaveLoggedNth(
1,
expect.objectContaining({
message: 'Hello, world!',
foo: { key1: 'bar', key2: 'baz' },
})
);
});

it('overrides temporary keys when the same key is added as persistent key', () => {
// Prepare
const logger = new Logger();
Expand Down Expand Up @@ -806,25 +863,27 @@ describe('Working with keys', () => {
);
});

it.each([{ value: null }, { value: undefined }])(
'handles null and undefined values when passing them to the log method ($value)',
({ value }) => {
// Prepare
const logger = new Logger();
it.each([
{ value: null },
{ value: undefined },
])('handles null and undefined values when passing them to the log method ($value)', ({
value,
}) => {
// Prepare
const logger = new Logger();

// Act
// @ts-expect-error - these values are already forbidden by TypeScript, but JavaScript-only customers might pass them
logger.info('foo', value);
// Act
// @ts-expect-error - these values are already forbidden by TypeScript, but JavaScript-only customers might pass them
logger.info('foo', value);

// Assess
expect(console.info).toHaveLoggedNth(
1,
expect.objectContaining({
message: 'foo',
})
);
}
);
// Assess
expect(console.info).toHaveLoggedNth(
1,
expect.objectContaining({
message: 'foo',
})
);
});

describe('deprecated persistentLogAttributes usage', () => {
it('sets #attributesStore on the logger', () => {
Expand Down
Loading