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): merge child logger options correctly #1178

48 changes: 12 additions & 36 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions packages/logger/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
"types": "./lib/index.d.ts",
"typedocMain": "src/index.ts",
"devDependencies": {
"@types/lodash.clonedeep": "^4.5.7",
"@types/lodash.merge": "^4.6.7",
"@types/lodash.pickby": "^4.6.7"
},
Expand All @@ -48,7 +47,6 @@
},
"dependencies": {
"@aws-lambda-powertools/commons": "^1.4.1",
"lodash.clonedeep": "^4.5.0",
"lodash.merge": "^4.6.2",
"lodash.pickby": "^4.6.0"
},
Expand Down
14 changes: 11 additions & 3 deletions packages/logger/src/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type { Context, Handler } from 'aws-lambda';
import { Utility } from '@aws-lambda-powertools/commons';
import { LogFormatterInterface, PowertoolLogFormatter } from './formatter';
import { LogItem } from './log';
import cloneDeep from 'lodash.clonedeep';
import merge from 'lodash.merge';
import { ConfigServiceInterface, EnvironmentVariablesService } from './config';
import { LogJsonIndent } from './types';
Expand Down Expand Up @@ -151,7 +150,6 @@ class Logger extends Utility implements ClassThatLogs {
*/
public constructor(options: ConstructorOptions = {}) {
super();

this.setOptions(options);
}

Expand Down Expand Up @@ -205,7 +203,17 @@ class Logger extends Utility implements ClassThatLogs {
* @returns {Logger}
*/
public createChild(options: ConstructorOptions = {}): Logger {
return cloneDeep(this).setOptions(options);
const parentsPowertoolsLogData = this.getPowertoolLogData();
const childLogger = new Logger(merge({}, parentsPowertoolsLogData, options));

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

if (parentsPowertoolsLogData.lambdaContext) {
childLogger.addContext(parentsPowertoolsLogData.lambdaContext as Context);
}

return childLogger;
}

/**
Expand Down
Loading