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

Error format doesn't work when including metadata #87

Open
filetvignon opened this issue Mar 14, 2019 · 3 comments
Open

Error format doesn't work when including metadata #87

filetvignon opened this issue Mar 14, 2019 · 3 comments

Comments

@filetvignon
Copy link

filetvignon commented Mar 14, 2019

I'm using the 'Errors' format and pipelining it through a custom printf format.
Basically I'm having enormous inconsistencies when I try to log my error with some metadata.
So logger.info(err) works fine, but logger.info(err, someMetaData) fails to give me the message or the stack, they both come as undefined when I log the "info" object that's coming in my custom format function.

What's more, this only happens if I DID NOT set a message for the Error in ins constructor, even if it's just an empty string.

So in summary, this gives me a headache:

let err = new Error();
err.message = 'Message set later';
logger.info(err, someMetaData);

But these work::

let err = new Error(); 
err.message = 'message set later';
logger.info(err);

let err = new Error('message in constructor'); // or let err = new Error('');
err.message = 'another message';
logger.info(err, someMetaData);

let err = new Error('Message in constructor');
err.message = 'another message';
logger.info(err);

Currently, I'm using this configuration for my logger:

const devPrint = info => {
  // This is the final format before info is printed on console
  // Gonna print info to normal console to compare
  console.log(info);
  let {level, message, timestamp, stack, ...metadata} = info;
  let print = `${timestamp}: ${level} ${message}\n`;
  if (metadata && Object.keys(metadata).length > 0) print += `METADATA ==> ${JSON.stringify(metadata, null, 2)}\n`;
  if (info.stack) print += `STACK ==> ${stack}\n`;
  return print;
};

  const options = {
    level: 'debug',
    format: winston.format.combine(
      winston.format.errors({stack: true}),
      winston.format.printf(devPrint)
    )
  };

  const logger = winston.createLogger({
    transports: [
      new winston.transports.Console(options)
    ]
  });

Is there something wrong with my configuration?

@filetvignon
Copy link
Author

filetvignon commented Mar 15, 2019

For anyone having the same issue, here's my workaround:
Add a custom format before calling winston.format.errors() and set 'message' property as NON ENUMERABLE if 'info' is an instance of error:

const options = {
    level: 'debug',
    format: winston.format.combine(
    // if this is an error, MUST set 'message' as NON ENUMERABLE, otherwise destruction WILL befall us
    // PS: sometimes winston will save the Error object as 'info', but other times as 'info.message', so check both
      winston.format(info => {
        if (info instanceof Error && info.propertyIsEnumerable('message')) {
          Object.defineProperty(info, 'message', {value: info.message, enumerable: false});
        } else if (info.message instanceof Error && info.message.propertyIsEnumerable('message')) {
          Object.defineProperty(info.message, 'message', {value: info.message.message, enumerable: false});
        }
        return info;
    })(),
      winston.format.errors({stack: true})
    )
  };

  const logger = winston.createLogger({
    transports: [
      new winston.transports.Console(options)
    ]
  });

@trashstack
Copy link

I think I ran into the same thing @filetvignon. I've opened a PR for what I think is a quick fix #92

@xeroxoid
Copy link

Any updates on this issue? It has been open since May 2019 and the actual issue still exists in the latest versions of winston & logform.

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

No branches or pull requests

3 participants