Skip to content

Bug: [BUG] Child instance cannot import settings from parent #146

Closed
@dever23b

Description

@dever23b

Describe the bug
The LoggerWithoutCallSite.settings getter method merges this._parentOrDefaultSettings and this._mySettings in a manner which results . As of this writing, the code uses spread operators to merge the two property sets; however, this method of merging simply overwrites and does not prevent a "falsy" value from overwriting a "truthy" value. Accordingly, settings that are left undefined in a child will still overwrite the corresponding setting from the parent. The merging process must consider whether a child setting is defined prior to overwriting the defaults, or the defaults will never be used.

To Reproduce
Steps to reproduce the behavior:

import { Logger } from 'tslog';
const parent = new Logger({ name: 'parent' }); // parent.settings.name = 'parent'
const child = parent.getChildLogger({ requestId: 'foo' }); // parent.settings.name = undefined

child.info('test'); 
// when eventually passed through prettyPrintLog, 
// parent.settings.name is evaluated to undefined and not printed

Expected behavior
Child logger settings should be merged with parent, as the documentation suggests, such that only defined settings overwrite parent/defaults.

Additional context

public get settings(): ISettings {
const myPrefix: unknown[] =
this._mySettings.prefix != null ? this._mySettings.prefix : [];
return {
...this._parentOrDefaultSettings,
...this._mySettings,
prefix: [...this._parentOrDefaultSettings.prefix, ...myPrefix],
};
}

Node.js Version
v16.16.0

Activity

terehov

terehov commented on Aug 23, 2022

@terehov
Contributor

@dever23b Thank you for reporting this bug.

However, the faulty code isn't the spread operator, it's rather here:

this._mySettings.name =
this._mySettings.name ??
(this._mySettings.setCallerAsLoggerName
? LoggerHelper.getCallSites()?.[0]?.getTypeName() ??
LoggerHelper.getCallSites()?.[0]?.getFunctionName() ??
undefined
: undefined);

It's the way the name gets replaced in certain scenarios. I have fixed it and going to publish a new version with some more bugfixes shortly.

added a commit that references this issue on Aug 23, 2022
dever23b

dever23b commented on Aug 23, 2022

@dever23b
Author

Thanks! I'll get updated and test this week.

dever23b

dever23b commented on Aug 25, 2022

@dever23b
Author

I pulled the latest version and tested this. It works! Thanks again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @terehov@dever23b

        Issue actions

          Bug: [BUG] Child instance cannot import settings from parent · Issue #146 · fullstack-build/tslog