Skip to content

Commit 84ab4b9

Browse files
authored
fix(logger): createChild not passing all parent's attributes (#1267)
* fix(logger): fix a bug when child logger did get all the parent's attributes * test(logger): update test, expect any boolean in logsSampled because it depends on random
1 parent ba921c1 commit 84ab4b9

File tree

2 files changed

+97
-4
lines changed

2 files changed

+97
-4
lines changed

Diff for: packages/logger/src/Logger.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,13 @@ class Logger extends Utility implements ClassThatLogs {
203203
* @returns {Logger}
204204
*/
205205
public createChild(options: ConstructorOptions = {}): Logger {
206+
const parentsOptions = {
207+
logLevel: this.getLogLevel(),
208+
customConfigService: this.getCustomConfigService(),
209+
logFormatter: this.getLogFormatter(),
210+
};
206211
const parentsPowertoolsLogData = this.getPowertoolLogData();
207-
const childLogger = new Logger(merge({}, parentsPowertoolsLogData, options));
212+
const childLogger = new Logger(merge(parentsOptions, parentsPowertoolsLogData, options));
208213

209214
const parentsPersistentLogAttributes = this.getPersistentLogAttributes();
210215
childLogger.addPersistentLogAttributes(parentsPersistentLogAttributes);

Diff for: packages/logger/tests/unit/Logger.test.ts

+91-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { ContextExamples as dummyContext, Events as dummyEvent, LambdaInterface
88
import { createLogger, Logger } from '../../src';
99
import { EnvironmentVariablesService } from '../../src/config';
1010
import { PowertoolLogFormatter } from '../../src/formatter';
11-
import { ClassThatLogs, LogJsonIndent } from '../../src/types';
11+
import { ClassThatLogs, LogJsonIndent, ConstructorOptions } from '../../src/types';
1212
import { Context } from 'aws-lambda';
1313
import { Console } from 'console';
1414

@@ -1297,7 +1297,7 @@ describe('Class: Logger', () => {
12971297

12981298
describe('Method: createChild', () => {
12991299

1300-
test('Child and grandchild loggers should have all ancestor\'s options', () => {
1300+
test('child and grandchild loggers should have all ancestor\'s options', () => {
13011301
// Prepare
13021302
const INDENTATION = LogJsonIndent.COMPACT;
13031303
const loggerOptions = {
@@ -1398,7 +1398,7 @@ describe('Class: Logger', () => {
13981398

13991399
});
14001400

1401-
test('when called, it returns a DISTINCT clone of the logger instance', () => {
1401+
test('child logger should be a DISTINCT clone of the logger instance', () => {
14021402

14031403
// Prepare
14041404
const INDENTATION = LogJsonIndent.COMPACT;
@@ -1716,6 +1716,94 @@ describe('Class: Logger', () => {
17161716
},
17171717
});
17181718
});
1719+
1720+
test('child logger should have parent\'s logFormatter', () => {
1721+
1722+
// Prepare
1723+
class MyCustomLogFormatter extends PowertoolLogFormatter {}
1724+
const parentLogger = new Logger({
1725+
logFormatter: new MyCustomLogFormatter()
1726+
});
1727+
1728+
// Act
1729+
const childLoggerWithParentLogFormatter = parentLogger.createChild();
1730+
1731+
// Assess
1732+
expect(childLoggerWithParentLogFormatter).toEqual(
1733+
expect.objectContaining({
1734+
logFormatter: expect.any(MyCustomLogFormatter),
1735+
})
1736+
);
1737+
});
1738+
1739+
test('child logger with custom logFormatter in options should have provided logFormatter', () => {
1740+
1741+
// Prepare
1742+
class MyCustomLogFormatter extends PowertoolLogFormatter {}
1743+
const parentLogger = new Logger();
1744+
1745+
// Act
1746+
const childLoggerWithCustomLogFormatter = parentLogger.createChild({
1747+
logFormatter: new MyCustomLogFormatter()
1748+
});
1749+
1750+
// Assess
1751+
expect(parentLogger).toEqual(
1752+
expect.objectContaining({
1753+
logFormatter: expect.any(PowertoolLogFormatter),
1754+
})
1755+
);
1756+
1757+
expect(childLoggerWithCustomLogFormatter).toEqual(
1758+
expect.objectContaining({
1759+
logFormatter: expect.any(MyCustomLogFormatter),
1760+
})
1761+
);
1762+
});
1763+
1764+
test('child logger should have exact same attributes as the parent logger created with all non-default options', () => {
1765+
1766+
// Prepare
1767+
class MyCustomLogFormatter extends PowertoolLogFormatter {}
1768+
class MyCustomEnvironmentVariablesService extends EnvironmentVariablesService {}
1769+
1770+
const options: ConstructorOptions = {
1771+
logLevel: 'ERROR',
1772+
serviceName: 'test-service-name',
1773+
sampleRateValue: 0.77,
1774+
logFormatter: new MyCustomLogFormatter(),
1775+
customConfigService: new MyCustomEnvironmentVariablesService(),
1776+
persistentLogAttributes: {
1777+
aws_account_id: '1234567890',
1778+
aws_region: 'eu-west-1',
1779+
},
1780+
environment: 'local'
1781+
};
1782+
const parentLogger = new Logger(options);
1783+
1784+
// Act
1785+
const childLogger = parentLogger.createChild();
1786+
1787+
// Assess
1788+
expect(childLogger).toEqual({
1789+
...parentLogger,
1790+
console: expect.any(Console),
1791+
logsSampled: expect.any(Boolean),
1792+
});
1793+
1794+
expect(childLogger).toEqual(
1795+
expect.objectContaining({
1796+
logFormatter: expect.any(MyCustomLogFormatter),
1797+
})
1798+
);
1799+
1800+
expect(childLogger).toEqual(
1801+
expect.objectContaining({
1802+
customConfigService: expect.any(MyCustomEnvironmentVariablesService),
1803+
})
1804+
);
1805+
1806+
});
17191807

17201808
});
17211809

0 commit comments

Comments
 (0)