Skip to content

Commit

Permalink
fix(logger): createChild not passing all parent's attributes (#1267)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
shdq committed Feb 9, 2023
1 parent ba921c1 commit 84ab4b9
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 4 deletions.
7 changes: 6 additions & 1 deletion packages/logger/src/Logger.ts
Expand Up @@ -203,8 +203,13 @@ class Logger extends Utility implements ClassThatLogs {
* @returns {Logger}
*/
public createChild(options: ConstructorOptions = {}): Logger {
const parentsOptions = {
logLevel: this.getLogLevel(),
customConfigService: this.getCustomConfigService(),
logFormatter: this.getLogFormatter(),
};
const parentsPowertoolsLogData = this.getPowertoolLogData();
const childLogger = new Logger(merge({}, parentsPowertoolsLogData, options));
const childLogger = new Logger(merge(parentsOptions, parentsPowertoolsLogData, options));

const parentsPersistentLogAttributes = this.getPersistentLogAttributes();
childLogger.addPersistentLogAttributes(parentsPersistentLogAttributes);
Expand Down
94 changes: 91 additions & 3 deletions packages/logger/tests/unit/Logger.test.ts
Expand Up @@ -8,7 +8,7 @@ import { ContextExamples as dummyContext, Events as dummyEvent, LambdaInterface
import { createLogger, Logger } from '../../src';
import { EnvironmentVariablesService } from '../../src/config';
import { PowertoolLogFormatter } from '../../src/formatter';
import { ClassThatLogs, LogJsonIndent } from '../../src/types';
import { ClassThatLogs, LogJsonIndent, ConstructorOptions } from '../../src/types';
import { Context } from 'aws-lambda';
import { Console } from 'console';

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

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

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

});

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

// Prepare
const INDENTATION = LogJsonIndent.COMPACT;
Expand Down Expand Up @@ -1716,6 +1716,94 @@ describe('Class: Logger', () => {
},
});
});

test('child logger should have parent\'s logFormatter', () => {

// Prepare
class MyCustomLogFormatter extends PowertoolLogFormatter {}
const parentLogger = new Logger({
logFormatter: new MyCustomLogFormatter()
});

// Act
const childLoggerWithParentLogFormatter = parentLogger.createChild();

// Assess
expect(childLoggerWithParentLogFormatter).toEqual(
expect.objectContaining({
logFormatter: expect.any(MyCustomLogFormatter),
})
);
});

test('child logger with custom logFormatter in options should have provided logFormatter', () => {

// Prepare
class MyCustomLogFormatter extends PowertoolLogFormatter {}
const parentLogger = new Logger();

// Act
const childLoggerWithCustomLogFormatter = parentLogger.createChild({
logFormatter: new MyCustomLogFormatter()
});

// Assess
expect(parentLogger).toEqual(
expect.objectContaining({
logFormatter: expect.any(PowertoolLogFormatter),
})
);

expect(childLoggerWithCustomLogFormatter).toEqual(
expect.objectContaining({
logFormatter: expect.any(MyCustomLogFormatter),
})
);
});

test('child logger should have exact same attributes as the parent logger created with all non-default options', () => {

// Prepare
class MyCustomLogFormatter extends PowertoolLogFormatter {}
class MyCustomEnvironmentVariablesService extends EnvironmentVariablesService {}

const options: ConstructorOptions = {
logLevel: 'ERROR',
serviceName: 'test-service-name',
sampleRateValue: 0.77,
logFormatter: new MyCustomLogFormatter(),
customConfigService: new MyCustomEnvironmentVariablesService(),
persistentLogAttributes: {
aws_account_id: '1234567890',
aws_region: 'eu-west-1',
},
environment: 'local'
};
const parentLogger = new Logger(options);

// Act
const childLogger = parentLogger.createChild();

// Assess
expect(childLogger).toEqual({
...parentLogger,
console: expect.any(Console),
logsSampled: expect.any(Boolean),
});

expect(childLogger).toEqual(
expect.objectContaining({
logFormatter: expect.any(MyCustomLogFormatter),
})
);

expect(childLogger).toEqual(
expect.objectContaining({
customConfigService: expect.any(MyCustomEnvironmentVariablesService),
})
);

});

});

Expand Down

0 comments on commit 84ab4b9

Please sign in to comment.