Skip to content

Commit edbbc4d

Browse files
committed
chore: refactor tests to isolate feature gap
1 parent dd404c0 commit edbbc4d

File tree

3 files changed

+126
-72
lines changed

3 files changed

+126
-72
lines changed

packages/logger/src/Logger.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -240,11 +240,13 @@ class Logger extends Utility implements LoggerInterface {
240240
/**
241241
* It adds the given persistent attributes (key-value pairs) to all log items generated by this Logger instance.
242242
*
243+
* @deprecated This method is deprecated and will be removed in the future major versions, please use {@link appendPersistentKeys()} instead.
244+
*
243245
* @param {LogAttributes} attributes
244246
* @returns {void}
245247
*/
246-
public addPersistentLogAttributes(attributes?: LogAttributes): void {
247-
merge(this.persistentLogAttributes, attributes);
248+
public addPersistentLogAttributes(attributes: LogAttributes): void {
249+
this.appendPersistentKeys(attributes);
248250
}
249251

250252
/**
@@ -257,6 +259,15 @@ class Logger extends Utility implements LoggerInterface {
257259
merge(this.temporaryLogAttributes, attributes);
258260
}
259261

262+
/**
263+
* It adds the given persistent attributes (key-value pairs) to all log items generated by this Logger instance.
264+
*
265+
* @param attributes - The attributes to add to all log items.
266+
*/
267+
public appendPersistentKeys(attributes: LogAttributes): void {
268+
merge(this.persistentLogAttributes, attributes);
269+
}
270+
260271
/**
261272
* It creates a separate Logger instance, identical to the current one
262273
* It's possible to overwrite the new instance options by passing them.
@@ -544,11 +555,11 @@ class Logger extends Utility implements LoggerInterface {
544555
}
545556

546557
/**
547-
* @deprecated This method is deprecated and will be removed in the future major versions.
548-
*
549558
* It sets the given attributes (key-value pairs) to all log items generated by this Logger instance.
550559
* Note: this replaces the pre-existing value.
551560
*
561+
* @deprecated This method is deprecated and will be removed in the future major versions, please use {@link appendPersistentKeys()} instead.
562+
*
552563
* @param {LogAttributes} attributes
553564
* @returns {void}
554565
*/
@@ -679,8 +690,8 @@ class Logger extends Utility implements LoggerInterface {
679690

680691
// gradually merge additional attributes starting from customer-provided persistent & temporary attributes
681692
let additionalLogAttributes = {
682-
...this.getPersistentLogAttributes(),
683693
...this.temporaryLogAttributes,
694+
...this.getPersistentLogAttributes(),
684695
};
685696
// if the main input is not a string, then it's an object with additional attributes, so we merge it
686697
additionalLogAttributes = merge(additionalLogAttributes, otherInput);

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

Lines changed: 98 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,15 +1476,9 @@ describe('Class: Logger', () => {
14761476

14771477
// Assess
14781478
expect(consoleSpy).toHaveBeenCalledTimes(1);
1479-
expect(consoleSpy).toHaveBeenNthCalledWith(
1480-
1,
1481-
JSON.stringify({
1482-
level: 'INFO',
1483-
message: 'This is an INFO log with some log attributes',
1484-
sampling_rate: 0,
1485-
service: 'hello-world',
1486-
timestamp: '2016-06-20T12:08:10.000Z',
1487-
xray_trace_id: '1-5759e988-bd862e3fe1be46a994272793',
1479+
const log = JSON.parse(consoleSpy.mock.calls[0][0]);
1480+
expect(log).toStrictEqual(
1481+
expect.objectContaining({
14881482
aws_account_id: '0987654321',
14891483
})
14901484
);
@@ -1758,6 +1752,52 @@ describe('Class: Logger', () => {
17581752
});
17591753
});
17601754

1755+
describe('Method: appendPersistentKeys', () => {
1756+
it('overwrites existing persistent keys with new ones', () => {
1757+
// Prepare
1758+
const logger = new Logger({
1759+
persistentKeys: {
1760+
aws_account_id: '123456789012',
1761+
},
1762+
});
1763+
1764+
// Act
1765+
logger.appendPersistentKeys({
1766+
aws_account_id: '0987654321',
1767+
});
1768+
1769+
// Assess
1770+
expect(logger).toEqual(
1771+
expect.objectContaining({
1772+
persistentLogAttributes: {
1773+
aws_account_id: '0987654321',
1774+
},
1775+
})
1776+
);
1777+
});
1778+
1779+
it('overwrites existing temporary keys with new ones in the next log', () => {
1780+
// Prepare
1781+
const logger = new Logger();
1782+
const debugSpy = jest.spyOn(logger['console'], 'info');
1783+
logger.appendKeys({
1784+
aws_account_id: '123456789012',
1785+
});
1786+
1787+
// Act
1788+
logger.appendPersistentKeys({
1789+
aws_account_id: '0987654321',
1790+
});
1791+
logger.info('This is an INFO log with some log attributes');
1792+
1793+
// Assess
1794+
const log = JSON.parse(debugSpy.mock.calls[0][0]);
1795+
expect(log).toStrictEqual(
1796+
expect.objectContaining({ aws_account_id: '0987654321' })
1797+
);
1798+
});
1799+
});
1800+
17611801
describe('Method: injectLambdaContext', () => {
17621802
beforeEach(() => {
17631803
jest.spyOn(console, 'log').mockImplementation(() => ({}));
@@ -1788,10 +1828,10 @@ describe('Class: Logger', () => {
17881828
await handler(event, context);
17891829

17901830
// Assess
1791-
expect(consoleSpy).toBeCalledTimes(1);
1792-
expect(consoleSpy).toHaveBeenNthCalledWith(
1793-
1,
1794-
JSON.stringify({
1831+
expect(consoleSpy).toHaveBeenCalledTimes(1);
1832+
const log = JSON.parse(consoleSpy.mock.calls[0][0]);
1833+
expect(log).toEqual(
1834+
expect.objectContaining({
17951835
cold_start: true,
17961836
function_arn:
17971837
'arn:aws:lambda:eu-west-1:123456789012:function:foo-bar-function',
@@ -1830,10 +1870,10 @@ describe('Class: Logger', () => {
18301870

18311871
// Assess
18321872

1833-
expect(consoleSpy).toBeCalledTimes(2);
1834-
expect(consoleSpy).toHaveBeenNthCalledWith(
1835-
1,
1836-
JSON.stringify({
1873+
expect(consoleSpy).toHaveBeenCalledTimes(2);
1874+
const log1 = JSON.parse(consoleSpy.mock.calls[0][0]);
1875+
expect(log1).toStrictEqual(
1876+
expect.objectContaining({
18371877
level: 'INFO',
18381878
message: 'An INFO log without context!',
18391879
sampling_rate: 0,
@@ -1842,9 +1882,9 @@ describe('Class: Logger', () => {
18421882
xray_trace_id: '1-5759e988-bd862e3fe1be46a994272793',
18431883
})
18441884
);
1845-
expect(consoleSpy).toHaveBeenNthCalledWith(
1846-
2,
1847-
JSON.stringify({
1885+
const log2 = JSON.parse(consoleSpy.mock.calls[1][0]);
1886+
expect(log2).toStrictEqual(
1887+
expect.objectContaining({
18481888
cold_start: true,
18491889
function_arn:
18501890
'arn:aws:lambda:eu-west-1:123456789012:function:foo-bar-function',
@@ -1887,10 +1927,10 @@ describe('Class: Logger', () => {
18871927
// Assess
18881928

18891929
expect(actualResult).toEqual(expectedReturnValue);
1890-
expect(consoleSpy).toBeCalledTimes(2);
1891-
expect(consoleSpy).toHaveBeenNthCalledWith(
1892-
1,
1893-
JSON.stringify({
1930+
expect(consoleSpy).toHaveBeenCalledTimes(2);
1931+
const log1 = JSON.parse(consoleSpy.mock.calls[0][0]);
1932+
expect(log1).toStrictEqual(
1933+
expect.objectContaining({
18941934
level: 'INFO',
18951935
message: 'An INFO log without context!',
18961936
sampling_rate: 0,
@@ -1899,9 +1939,9 @@ describe('Class: Logger', () => {
18991939
xray_trace_id: '1-5759e988-bd862e3fe1be46a994272793',
19001940
})
19011941
);
1902-
expect(consoleSpy).toHaveBeenNthCalledWith(
1903-
2,
1904-
JSON.stringify({
1942+
const log2 = JSON.parse(consoleSpy.mock.calls[1][0]);
1943+
expect(log2).toStrictEqual(
1944+
expect.objectContaining({
19051945
cold_start: true,
19061946
function_arn:
19071947
'arn:aws:lambda:eu-west-1:123456789012:function:foo-bar-function',
@@ -1953,8 +1993,9 @@ describe('Class: Logger', () => {
19531993

19541994
// Assess
19551995
expect(debugSpy).toHaveBeenCalledTimes(2);
1956-
expect(debugSpy).toHaveBeenCalledWith(
1957-
JSON.stringify({
1996+
const log1 = JSON.parse(debugSpy.mock.calls[0][0]);
1997+
expect(log1).toStrictEqual(
1998+
expect.objectContaining({
19581999
cold_start: true,
19592000
function_arn:
19602001
'arn:aws:lambda:eu-west-1:123456789012:function:foo-bar-function',
@@ -1972,8 +2013,9 @@ describe('Class: Logger', () => {
19722013
details: { user_id: '1234' },
19732014
})
19742015
);
1975-
expect(debugSpy).toHaveBeenLastCalledWith(
1976-
JSON.stringify({
2016+
const log2 = JSON.parse(debugSpy.mock.calls[1][0]);
2017+
expect(log2).toStrictEqual(
2018+
expect.objectContaining({
19772019
cold_start: true,
19782020
function_arn:
19792021
'arn:aws:lambda:eu-west-1:123456789012:function:foo-bar-function',
@@ -1988,8 +2030,6 @@ describe('Class: Logger', () => {
19882030
xray_trace_id: '1-5759e988-bd862e3fe1be46a994272793',
19892031
})
19902032
);
1991-
1992-
debugSpy.mockRestore();
19932033
});
19942034

19952035
test('when clearState is enabled, the persistent log attributes added in the handler ARE NOT cleared when the method returns', async () => {
@@ -2008,7 +2048,7 @@ describe('Class: Logger', () => {
20082048
_context: Context
20092049
): Promise<void> {
20102050
// These persistent attributes stay persistent
2011-
logger.addPersistentLogAttributes({
2051+
logger.appendPersistentKeys({
20122052
details: { user_id: '1234' },
20132053
});
20142054
logger.debug('This is a DEBUG log with the user_id');
@@ -2054,7 +2094,7 @@ describe('Class: Logger', () => {
20542094
_context: Context
20552095
): Promise<void> {
20562096
// This key is persistent and will stay persistent
2057-
logger.addPersistentLogAttributes({
2097+
logger.appendPersistentKeys({
20582098
foo: 'bar',
20592099
});
20602100
// This attribute is temporary and will be cleared
@@ -2075,8 +2115,9 @@ describe('Class: Logger', () => {
20752115

20762116
// Assess
20772117
expect(debugSpy).toHaveBeenCalledTimes(2);
2078-
expect(debugSpy).toHaveBeenCalledWith(
2079-
JSON.stringify({
2118+
const log1 = JSON.parse(debugSpy.mock.calls[0][0]);
2119+
expect(log1).toStrictEqual(
2120+
expect.objectContaining({
20802121
cold_start: true,
20812122
function_arn:
20822123
'arn:aws:lambda:eu-west-1:123456789012:function:foo-bar-function',
@@ -2093,8 +2134,9 @@ describe('Class: Logger', () => {
20932134
biz: 'baz',
20942135
})
20952136
);
2096-
expect(debugSpy).toHaveBeenLastCalledWith(
2097-
JSON.stringify({
2137+
const log2 = JSON.parse(debugSpy.mock.calls[1][0]);
2138+
expect(log2).toStrictEqual(
2139+
expect.objectContaining({
20982140
cold_start: true,
20992141
function_arn:
21002142
'arn:aws:lambda:eu-west-1:123456789012:function:foo-bar-function',
@@ -2129,7 +2171,7 @@ describe('Class: Logger', () => {
21292171
_context: Context
21302172
): Promise<string> {
21312173
// This key is persistent and will stay persistent
2132-
logger.addPersistentLogAttributes({
2174+
logger.appendPersistentKeys({
21332175
foo: 'bar',
21342176
});
21352177
// This attribute is temporary and will be cleared
@@ -2149,8 +2191,9 @@ describe('Class: Logger', () => {
21492191
await expect(handler(event, context)).rejects.toThrow();
21502192

21512193
expect(debugSpy).toHaveBeenCalledTimes(1);
2152-
expect(debugSpy).toHaveBeenCalledWith(
2153-
JSON.stringify({
2194+
const log = JSON.parse(debugSpy.mock.calls[0][0]);
2195+
expect(log).toStrictEqual(
2196+
expect.objectContaining({
21542197
cold_start: true,
21552198
function_arn:
21562199
'arn:aws:lambda:eu-west-1:123456789012:function:foo-bar-function',
@@ -2202,10 +2245,10 @@ describe('Class: Logger', () => {
22022245
await handler(event, context);
22032246

22042247
// Assess
2205-
expect(consoleSpy).toBeCalledTimes(1);
2206-
expect(consoleSpy).toHaveBeenNthCalledWith(
2207-
1,
2208-
JSON.stringify({
2248+
expect(consoleSpy).toHaveBeenCalledTimes(1);
2249+
const log = JSON.parse(consoleSpy.mock.calls[0][0]);
2250+
expect(log).toStrictEqual(
2251+
expect.objectContaining({
22092252
cold_start: true,
22102253
function_arn:
22112254
'arn:aws:lambda:eu-west-1:123456789012:function:foo-bar-function',
@@ -2249,10 +2292,10 @@ describe('Class: Logger', () => {
22492292
await handler(event, context);
22502293

22512294
// Assess
2252-
expect(consoleSpy).toBeCalledTimes(1);
2253-
expect(consoleSpy).toHaveBeenNthCalledWith(
2254-
1,
2255-
JSON.stringify({
2295+
expect(consoleSpy).toHaveBeenCalledTimes(1);
2296+
const log = JSON.parse(consoleSpy.mock.calls[0][0]);
2297+
expect(log).toStrictEqual(
2298+
expect.objectContaining({
22562299
cold_start: true,
22572300
function_arn:
22582301
'arn:aws:lambda:eu-west-1:123456789012:function:foo-bar-function',
@@ -2307,10 +2350,10 @@ describe('Class: Logger', () => {
23072350
await handler({}, context);
23082351

23092352
// Assess
2310-
expect(consoleSpy).toBeCalledTimes(1);
2311-
expect(consoleSpy).toHaveBeenNthCalledWith(
2312-
1,
2313-
JSON.stringify({
2353+
expect(consoleSpy).toHaveBeenCalledTimes(1);
2354+
const log = JSON.parse(consoleSpy.mock.calls[0][0]);
2355+
expect(log).toStrictEqual(
2356+
expect.objectContaining({
23142357
cold_start: true,
23152358
function_arn:
23162359
'arn:aws:lambda:eu-west-1:123456789012:function:foo-bar-function',
@@ -2704,7 +2747,7 @@ describe('Class: Logger', () => {
27042747
const childLogger = parentLogger.createChild();
27052748

27062749
// Act
2707-
parentLogger.addPersistentLogAttributes({
2750+
parentLogger.appendPersistentKeys({
27082751
aws_account_id: '123456789012',
27092752
aws_region: 'eu-west-1',
27102753
logger: {

0 commit comments

Comments
 (0)