#2381·winston

[Bug]: Child logger changes Error object type to plain which cause the info object to bypass error() formatter

Author: maxkoretskyiCreated Dec 21, 2023Updated Apr 11, 2026
LabelsNeeds InvestigationBug

Search Terms

Child logger

The problem

Logger defined like this:

const logger = winston.createLogger({
   format: combine(
     error({stack: true, cause: true}),
    format(info) => {
        return info.stack +  info.cause;
   ],
     transports: [
      fileRotateTransport,
      new winston.transports.Console({
          format: formatter
      })
  ],

const childLogger = logger.child({ service: 'child_meta });
childLogger .error(new Error('msg', {cause: {location: 3}}));

Error formatter uses info instanceof Error check to apply it's logic:

module.exports = format((einfo, { stack, cause }) => {
  if (einfo instanceof Error) {
    const info = Object.assign({}, einfo, {
      level: einfo.level,
      [LEVEL]: einfo[LEVEL] || einfo.level,
      message: einfo.message,
      [MESSAGE]: einfo[MESSAGE] || einfo.message
    });

    if (stack) info.stack = einfo.stack;
    if (cause) info.cause = einfo.cause;
    return info;
  }

But the child logger here changes the type:

class Logger extends Transform {
  constructor(options) {
    super({ objectMode: true });
    this.configure(options);
  }

  child(defaultRequestMetadata) {
    const logger = this;
    return Object.create(logger, {
      write: {
        value: function (info) {
          // here the type of infoClone no longer instance of Error
          const infoClone = Object.assign(
            {},
            defaultRequestMetadata,
            info
          );

so the error formatter is not applied at all.

I can workaround this by putting the error into the message property, but not sure that's how it should be done:

childLogger.error({message: new Error('some', {cause: e})});

due to this check:

  if (!(einfo.message instanceof Error)) return einfo;

What version of Winston presents the issue?

3.11.0

What version of Node are you using?

v16.20.0

If this worked in a previous version of Winston, which was it?

No response

Minimum Working Example

No response

Additional information

No response