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

winston.rejections returns undefined #1955

Open
1 task done
Suren55 opened this issue Sep 26, 2021 · 3 comments
Open
1 task done

winston.rejections returns undefined #1955

Suren55 opened this issue Sep 26, 2021 · 3 comments

Comments

@Suren55
Copy link

Suren55 commented Sep 26, 2021

Please tell us about your environment:

  • winston version?
    • winston@3
  • node -v outputs: 14.16.1
  • Operating System? Windows
  • Language? ES6

What is the problem?

winston.rejections is undefined when trying to call:

winston.rejections.handle( new winston.transports.File({ filename: 'rejections.log' }) );

What do you expect to happen instead?

I expect to be able to make the above mentioned call and log the error.

@DiniFarb
Copy link

Hello @Suren55

I think there is a mistake in the docs:
image

this should be the logger and not the winston itself.

Maybe you could do something like:

const { createLogger, transports, format } = require('winston');

const logger = createLogger({
    format: format.combine(
        format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss:ms' }),
        format.printf(info => `${info.timestamp} ${info.level}: ${info.message}`)
    ),
    transports: [
        new transports.Console(),
    ]
});

logger.rejections.handle(
    new transports.File({ filename: 'rejections.log' })
);

logger.info("Start ...");

const myPromise = new Promise((resolve, reject) => {
    reject('Ouch');
});

myPromise.then(() => {
    console.log('Hello World');
});

@Suren55
Copy link
Author

Suren55 commented Sep 30, 2021

Hi @andreasvogt89. Thank you for the reply, however I am not sure that it's a mistake in the docs, because the same logic exists for winston.exceptions.handle() and it works just fine.

Also, thank you for the piece of code you provided, but I did not want to create a custom logger, I wanted to use the default one for now.

@DiniFarb
Copy link

DiniFarb commented Oct 1, 2021

Yes you are right, it does not work for the default logger.

The problem is that rejections which is the RejectionHandler object is not exposed to the winston.

If I try to use the alternativ way like below, it caught the UnhandledPromiseRejection but did not write it to the file.

const winston = require('winston');

winston.add(new winston.transports.File({
    filename: 'rejections.log',
    handleRejections: true
}));
    
winston.info('Starting. . . ');

const myPromise = new Promise((resolve, reject) => {
    reject('Ouch');
});

myPromise.then(() => {
    console.log('Hello World');
});

I think best way to solve would be to add rejections to the default property exposer in winston.js so the rejections can be used with winston (defaultLogger)

/**
 * Define getters / setters for appropriate properties of the default logger
 * which need to be exposed by winston.
 * @type {Logger}
 */
['exitOnError', 'rejections'].forEach(prop => {
  Object.defineProperty(winston, prop, {
    get() {
      return defaultLogger[prop];
    },
    set(val) {
      defaultLogger[prop] = val;
    }
  });
});

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

2 participants