Skip to content

Info prototype destroyed by child logger #2577

@Foltys

Description

@Foltys

child(defaultRequestMetadata) {
const logger = this;
return Object.create(logger, {
write: {
value: function (info) {
const infoClone = Object.assign(
{},
defaultRequestMetadata,
info
);
// Object.assign doesn't copy inherited Error
// properties so we have to do that explicitly
//
// Remark (indexzero): we should remove this
// since the errors format will handle this case.
//
if (info instanceof Error) {
infoClone.stack = info.stack;
infoClone.message = info.message;
}
logger.write(infoClone);
}
}
});
}

Hello, based on the code here, the prototype chain is lost for any info object being passed in.

If I want to for example use Error objects in the log method and based on their prototype (using instanceof) apply some mapping, creating a child logger would remove this prototype because the Object.assign just copies it's enumerable properties.
Would it be possible to add this line inside the if, or perhaps some more elegant solution?
infoClone.__proto__ = info.__proto__;

The reason why I believe this would be fine is that there is no guarantee of not modifying the original object in case you are not using the child method anyway.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions