Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(logger): fix logger attribute merging #535

Merged
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 2 additions & 2 deletions packages/logger/src/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ class Logger implements ClassThatLogs {
* @returns {void}
*/
public addPersistentLogAttributes(attributes?: LogAttributes): void {
this.persistentLogAttributes = merge(attributes, this.getPersistentLogAttributes());
merge(this.persistentLogAttributes, attributes);
okoskine marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down Expand Up @@ -362,7 +362,7 @@ class Logger implements ClassThatLogs {
*/
private addToPowertoolLogData(...attributesArray: Array<Partial<PowertoolLogData>>): void {
attributesArray.forEach((attributes: Partial<PowertoolLogData>) => {
this.powertoolLogData = merge(attributes, this.getPowertoolLogData());
merge(this.powertoolLogData, attributes);
});
}

Expand Down
105 changes: 91 additions & 14 deletions packages/logger/tests/unit/Logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,26 +439,28 @@ describe('Class: Logger', () => {

describe('Method: addContext', () => {

const baseContext = {
callbackWaitsForEmptyEventLoop: true,
functionVersion: '$LATEST',
functionName: 'foo-bar-function-with-cold-start',
memoryLimitInMB: '128',
logGroupName: '/aws/lambda/foo-bar-function-with-cold-start',
logStreamName: '2021/03/09/[$LATEST]abcdef123456abcdef123456abcdef123456',
invokedFunctionArn: 'arn:aws:lambda:eu-central-1:123456789012:function:foo-bar-function-with-cold-start',
awsRequestId: 'c6af9ac6-7b61-11e6-9a41-93e812345678',
getRemainingTimeInMillis: () => 1234,
done: () => console.log('Done!'),
fail: () => console.log('Failed!'),
succeed: () => console.log('Succeeded!'),
};

test('when called during a COLD START invocation, it populates the logger\'s PowertoolLogData object with coldstart set to true', () => {

// Prepare
const logger = new Logger();

// Act
logger.addContext( {
callbackWaitsForEmptyEventLoop: true,
functionVersion: '$LATEST',
functionName: 'foo-bar-function-with-cold-start',
memoryLimitInMB: '128',
logGroupName: '/aws/lambda/foo-bar-function-with-cold-start',
logStreamName: '2021/03/09/[$LATEST]abcdef123456abcdef123456abcdef123456',
invokedFunctionArn: 'arn:aws:lambda:eu-central-1:123456789012:function:foo-bar-function-with-cold-start',
awsRequestId: 'c6af9ac6-7b61-11e6-9a41-93e812345678',
getRemainingTimeInMillis: () => 1234,
done: () => console.log('Done!'),
fail: () => console.log('Failed!'),
succeed: () => console.log('Succeeded!'),
});
logger.addContext({ ...baseContext });
okoskine marked this conversation as resolved.
Show resolved Hide resolved

// Assess
expect(logger).toEqual({
Expand Down Expand Up @@ -492,6 +494,44 @@ describe('Class: Logger', () => {
});
});

test('when called with a context object, the object is not mutated', () => {
okoskine marked this conversation as resolved.
Show resolved Hide resolved

// Prepare
const logger = new Logger();
const context1 = { ...baseContext, awsRequestId: 'c6af9ac6-7b61-11e6-9a41-93e812345678' };
const context2 = { ...baseContext, awsRequestId: 'd40c98a9-91c4-478c-a179-433c4b978289' };

// Act
logger.addContext(context1);
logger.addContext(context2);

// Assess
expect(context1.awsRequestId).toEqual('c6af9ac6-7b61-11e6-9a41-93e812345678');
expect(context2.awsRequestId).toEqual('d40c98a9-91c4-478c-a179-433c4b978289');
});

test('when called multiple times, the newer values override earlier values', () => {

// Prepare
const logger = new Logger();
const context1 = { ...baseContext, awsRequestId: 'c6af9ac6-7b61-11e6-9a41-93e812345678' };
const context2 = { ...baseContext, awsRequestId: 'd40c98a9-91c4-478c-a179-433c4b978289' };

// Act
logger.addContext(context1);
logger.addContext(context2);

// Assess
expect(logger).toEqual(
expect.objectContaining({
powertoolLogData: expect.objectContaining({
lambdaContext: expect.objectContaining({
awsRequestId: context2.awsRequestId,
})
})
})
);
});
});

describe('Method: appendKeys', () => {
Expand Down Expand Up @@ -523,6 +563,43 @@ describe('Class: Logger', () => {
},
}));
});

test('when called with user-provided attribute objects, the objects are not mutated', () => {

// Prepare
const logger = new Logger();
const attributes1 = { keyOne: 'abc' };
const attributes2 = { keyTwo: 'def' };

// Act
logger.appendKeys(attributes1);
logger.appendKeys(attributes2);

// Assess
expect(attributes1).toEqual({ keyOne: 'abc' });
expect(attributes2).toEqual({ keyTwo: 'def' });
});

test('when called multiple times, the newer values override earlier values', () => {

// Prepare
const logger = new Logger();

// Act
logger.appendKeys({
duplicateKey: 'one'
});
logger.appendKeys({
duplicateKey: 'two'
});

// Assess
expect(logger).toEqual(expect.objectContaining({
persistentLogAttributes: {
duplicateKey: 'two'
}
}));
});
});

describe('Method: createChild', () => {
Expand Down