Skip to content

fix(logger): fix a bug when child logger didn't get all the parent's attributes #1267

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

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
7 changes: 6 additions & 1 deletion packages/logger/src/Logger.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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