tslog icon indicating copy to clipboard operation
tslog copied to clipboard

Bug: Child loggers' settings changes influence parent and siblings

Open fezjo opened this issue 3 years ago • 0 comments

Describe the bug Modifying some settings in child loggers changes this setting also in all other loggers in hierarchy tree. The reason is shallow copy of this.settings in LoggerWithoutCallSite.getChildLogger. Because of this, methods like attachTransport which don't reassign but only modify settings fields also affect other loggers. Temporary solution is to manually initialize or copy relevant setting fields when creating child loggers.

To Reproduce Using example code for creating child loggers and adding transports:

import { ILogObject, Logger } from "tslog";

const loggerParent = new Logger({ name: "parent" });
const loggerChild1 = loggerParent.getChildLogger({ name: "child1" });
const loggerChild2 = loggerParent.getChildLogger({ name: "child2" });

const log = (logObject: ILogObject) => { console.log("child1 transport", logObject.argumentsArray) }
loggerChild1.attachTransport(
    { silly: log, debug: log, trace: log, info: log, warn: log, error: log, fatal: log },
    "debug"
);

loggerParent.info("parent")
loggerChild1.info("child1")
loggerChild2.info("child2");

We get output

2022-08-02 13:47:13.169  INFO  [parent src/index.ts:131 Object.<anonymous>] parent 
child1 transport [ 'parent' ]
2022-08-02 13:47:13.173  INFO  [parent src/index.ts:132 Object.<anonymous>] child1 
child1 transport [ 'child1' ]
2022-08-02 13:47:13.175  INFO  [child2 src/index.ts:133 Object.<anonymous>] child2 
child1 transport [ 'child2' ]

Expected behavior I would expect that adding transport to child logger would not affect its parent and definitely not other child loggers. Changing majority of the settings does not affect other loggers, therefore this behavior is surprising.

fezjo avatar Aug 02 '22 13:08 fezjo