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 does not log errors #1758

Open
1 of 2 tasks
Slyke opened this issue Feb 2, 2020 · 1 comment
Open
1 of 2 tasks

Winston does not log errors #1758

Slyke opened this issue Feb 2, 2020 · 1 comment

Comments

@Slyke
Copy link

Slyke commented Feb 2, 2020

Please tell us about your environment:

  • winston version?
    • winston@2
    • winston@3
  • node -v outputs: v13.1.0
  • Operating System? (Windows, macOS, or Linux) Linux
  • Language? (all | TypeScript X.X | ES6/7 | ES5 | Dart) ES6

What is the problem?

Winston does not log errors, nor catch exceptions.

What do you expect to happen instead?

Winston to catch and log errors and exceptions

Other information

Here's my Winston setup:

  const setupLogger = () => {
    try {
      const logger = require('winston');
      const path = require('path');
      const logPath = path.normalize(`${__dirname}/logs/`);
      console.log(`Log path ${logPath}`);

      const formatMessage = (theLog) => {
        if (theLog instanceof Error) {
          return `${theLog.timestamp} ${theLog.level}: ${theLog.message} - StackTrace: ${theLog.stack}`;
        } else {
          return `${theLog.timestamp} ${theLog.level}: ${theLog.message}`;
        }
      };

      const transportDefaults = {
        handleExceptions: true,
        exitOnError: false
      };

      const loggerInstance = logger.createLogger({
        level: 'info',
        format: logger.format.combine(
          logger.format.printf((theLog) => `${theLog.timestamp} ${theLog.level}: ${theLog.message}`),
          logger.format.timestamp(),
          logger.format.json()
        ),
        transports: [
          new logger.transports.File({ filename: `${logPath}combined.log`, ...transportDefaults }),
          new logger.transports.File({ filename: `${logPath}error.log`, level: 'error', ...transportDefaults }),
          new logger.transports.File({ filename: `${logPath}warn.log`, level: 'warn', ...transportDefaults }),
          new logger.transports.File({ filename: `${logPath}info.log`, level: 'info', ...transportDefaults }),
          new logger.transports.File({ filename: `${logPath}debug.log`, level: 'debug', ...transportDefaults })
        ],
        exceptionHandlers: [
          new logger.transports.File({ filename: `${logPath}exceptions.log`, ...transportDefaults })
        ]
      });
    
      if (process.env.NODE_ENV !== 'production') {
        loggerInstance.add(new logger.transports.Console({
          handleExceptions: true,
          exitOnError: false,
          format: logger.format.combine(
            logger.format.errors({ stack: true }),
            logger.format.colorize(),
            logger.format.align(),
            logger.format.printf((theLog) => formatMessage(theLog)),
          )
        }));
        console.log('Logging to console');
      }

      return loggerInstance;
    
    } catch (err) {
      console.log("Error setting up logger: ", err);
      process.exit(5);
    }
  };

If an exception happens, nothing is saved to any file, nor is it printed to console. It's as if everything is fine and nothing erred.

If I Logger.error(err) in an exception block, then this is printed to file:

{"level":"error","timestamp":"2020-02-02T06:56:07.973Z"}

And this is printed to console:

2020-02-02T06:56:07.973Z error:         undefined
@c-vetter
Copy link

Thanks to #1771 (comment), I found that you need to add the errors formatter to the logger itself. It does not work on the transport.

So, instead of this:

const log = winston.createLogger({
	transports: [new winston.transports.Console({
		format: combine(
			errors(),
			stringify(),
			cli(),
		),
	})],
})

This is necessary:

const log = winston.createLogger({
	format: errors(),
	transports: [new winston.transports.Console({
		format: combine(
			stringify(),
			cli(),
		),
	})],
})

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