Skip to content

fix(logger): merge child logger options correctly #1178

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

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
11 changes: 8 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,14 @@ 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);
const childPowertoolsLogData = childLogger.getPowertoolLogData();
childLogger.addToPowertoolLogData(merge({}, parentsPowertoolsLogData, childPowertoolsLogData));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shdq: question: why do we merge and use the parentsPowertoolsLogData twice? Maybe worth documenting with a comment (inside the method, so we don't expose it in the docstring)

Copy link
Contributor Author

@shdq shdq Nov 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Constructor doesn't use lambdaContext, so in line 211 it extracts context (if any) and merges it with childPowertoolsLogData to not overwrite the child's options. Thanks for the question, I went through the code again and refactored it, removed extra work, and made it more readable – no comments needed:

const parentsLambdaContext = parentsPowertoolsLogData?.lambdaContext;
if (parentsLambdaContext) {
  childLogger.addToPowertoolLogData({ lambdaContext: parentsLambdaContext });
}

I will push it when I fix the test coverage.


return childLogger;
}

/**
Expand Down
Loading