Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Child Logger Doesn't Override Parent Metadata #1788

Closed
1 of 2 tasks
mplover opened this issue Apr 25, 2020 · 3 comments
Closed
1 of 2 tasks

Child Logger Doesn't Override Parent Metadata #1788

mplover opened this issue Apr 25, 2020 · 3 comments

Comments

@mplover
Copy link

mplover commented Apr 25, 2020

Please tell us about your environment:

  • winston version?
    • winston@2
    • winston@3
  • node -v outputs: v13.13.0
  • Operating System? Linux
  • Language? TypeScript 3.8

What is the problem?

The documentation states:

You can create child loggers from existing loggers to pass metadata overrides

But the metadata specified in defaultMeta in createLogger is not overridden.

What do you expect to happen instead?

I expect the metadata (options) provided to the child logger to override the defaultMeta provided in an upstream logger

Other information

Looking at the Logger class (logger.js; Line 58), the write method overridden by the child uses Object.assign() to merge the info and child metadata objects, but they appear to be in the wrong order.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign#Merging_objects_with_same_properties

defaultRequestMetadata should come after info in the sources list to allow for child metadata to override the parent logger's metadata.

@NtsDK
Copy link

NtsDK commented Nov 18, 2020

I also want to use child loggers to get more log control. And got this problem too.
I use such workaround. Maybe it will be useful for somebody.

function customChild(logger, defaultMeta) {
  const childLogger = logger.child();
  childLogger.defaultMeta = { ...logger.defaultMeta, ...defaultMeta };
  childLogger.customChild = customChild;
  return childLogger;
}

winstonLogger.customChild = customChild;

Also documentation about child loggers is poor. It has no example of using defaultMeta usage in log messages and there is no child logger examples in examples folder.

@jrasm91
Copy link

jrasm91 commented Mar 4, 2021

I am having this same problem. Is there a reason why this hasn't been fixed already? Seems like a very simple PR to swap the order of requestDefaultMetadata and info.

https://github.com/winstonjs/winston/blob/master/lib/winston/logger.js#L52-L53

@maverick1872
Copy link
Member

Closing as this issue is being consolidated into #2029

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

Successfully merging a pull request may close this issue.

4 participants