Skip to content

Commit cb91374

Browse files
authored
fix(logger): merge child logger options correctly (#1178)
* 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
1 parent cb9eaf9 commit cb91374

File tree

5 files changed

+312
-48
lines changed

5 files changed

+312
-48
lines changed

package-lock.json

Lines changed: 12 additions & 36 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/logger/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
"types": "./lib/index.d.ts",
3333
"typedocMain": "src/index.ts",
3434
"devDependencies": {
35-
"@types/lodash.clonedeep": "^4.5.7",
3635
"@types/lodash.merge": "^4.6.7",
3736
"@types/lodash.pickby": "^4.6.7"
3837
},
@@ -48,7 +47,6 @@
4847
},
4948
"dependencies": {
5049
"@aws-lambda-powertools/commons": "^1.4.1",
51-
"lodash.clonedeep": "^4.5.0",
5250
"lodash.merge": "^4.6.2",
5351
"lodash.pickby": "^4.6.0"
5452
},

packages/logger/src/Logger.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import type { Context, Handler } from 'aws-lambda';
33
import { Utility } from '@aws-lambda-powertools/commons';
44
import { LogFormatterInterface, PowertoolLogFormatter } from './formatter';
55
import { LogItem } from './log';
6-
import cloneDeep from 'lodash.clonedeep';
76
import merge from 'lodash.merge';
87
import { ConfigServiceInterface, EnvironmentVariablesService } from './config';
98
import { LogJsonIndent } from './types';
@@ -151,7 +150,6 @@ class Logger extends Utility implements ClassThatLogs {
151150
*/
152151
public constructor(options: ConstructorOptions = {}) {
153152
super();
154-
155153
this.setOptions(options);
156154
}
157155

@@ -205,7 +203,17 @@ class Logger extends Utility implements ClassThatLogs {
205203
* @returns {Logger}
206204
*/
207205
public createChild(options: ConstructorOptions = {}): Logger {
208-
return cloneDeep(this).setOptions(options);
206+
const parentsPowertoolsLogData = this.getPowertoolLogData();
207+
const childLogger = new Logger(merge({}, parentsPowertoolsLogData, options));
208+
209+
const parentsPersistentLogAttributes = this.getPersistentLogAttributes();
210+
childLogger.addPersistentLogAttributes(parentsPersistentLogAttributes);
211+
212+
if (parentsPowertoolsLogData.lambdaContext) {
213+
childLogger.addContext(parentsPowertoolsLogData.lambdaContext as Context);
214+
}
215+
216+
return childLogger;
209217
}
210218

211219
/**

0 commit comments

Comments
 (0)