Skip to content

Commit

Permalink
fix(logger): merge child logger options correctly (#1178)
Browse files Browse the repository at this point in the history
* bug(logger): fix bug when child logger with overwritten options clears all other options

* test(logger): add tests for createChild method

* refactor(logger): remove clonedeep dependency

* refactor(logger): move initOptions initializer to setOptions

* feat(logger): add addPersistentLogAttributes from parent to child logger

* test(logger): add tests for createChild with persistent log attrs

* feat(logger): added addContext support, remove redundant initOption

* refactor(logger): add parent's context to child explicitly, without merging
  • Loading branch information
shdq authored Nov 23, 2022
1 parent cb9eaf9 commit cb91374
Show file tree
Hide file tree
Showing 5 changed files with 312 additions and 48 deletions.
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

0 comments on commit cb91374

Please sign in to comment.