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

Log an error without stack and other properties #1451

Closed
fengxinming opened this issue Aug 27, 2018 · 4 comments
Closed

Log an error without stack and other properties #1451

fengxinming opened this issue Aug 27, 2018 · 4 comments

Comments

@fengxinming
Copy link

winston@3

module.exports = format((info, opts) => {
  // ...
  return info;
});

When I log an error, and then the var info become an error, I expect the info.message is error

@DABH
Copy link
Contributor

DABH commented Aug 27, 2018

This is by design. If I understand correctly, you just want to log the error message without the other error properties. In that case, I would recommend just logging err.message or whatever property you desire, rather than logging the entire error object.

@DABH DABH closed this as completed Aug 27, 2018
@aegyed91
Copy link

aegyed91 commented Sep 3, 2018

@DABH what to do if i want to log an error with stack trace and other properties?

logger.error(new Error('whatever'))

Is only going to log "whatever" and i am losing my stack trace. Because of this line: https://github.com/winstonjs/winston/blob/master/lib/winston/create-logger.js#L52

@DABH
Copy link
Contributor

DABH commented Sep 3, 2018

I think there is an unresolved issue there. I'm looking into it. Looks like this is the kind of workaround you need to do for now since Error properties are non-enumerable for some reason (thanks JS designers):

const winston = require('winston');
const format = winston.format;

const enumerateErrorFormat = format(info => {
  if (info.message instanceof Error) {
    info.message = Object.assign({
      message: info.message.message,
      stack: info.message.stack
    }, info.message);
  }

  if (info instanceof Error) {
    return Object.assign({
      message: info.message,
      stack: info.stack
    }, info);
  }

  return info;
});

const logger = winston.createLogger({
  level: 'debug',
  format: format.combine(
    enumerateErrorFormat(),
    format.json()
  ),
  transports: [
    new winston.transports.Console(),
  ],
});

logger.log('error', new Error('whatever'));

See #1338 . I'll see what I can do about figuring out a better solution for Errors that doesn't require this extra format snippet.

@aegyed91
Copy link

aegyed91 commented Sep 3, 2018

Thx @DABH for pointing to the correct issue, indeed this workaround seem to work.

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