question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error logging method does not logs Error instance correctly

See original GitHub issue

Please tell us about your environment:

  • winston version? 3.1.0

    • winston@2
    • winston@3
  • node -v outputs: v8.11.3

  • Operating System? Linux

  • Language? all

What is the problem?

logger.error called with Error instance doesn’t use message and stack fields, instead logs test error.

Code example:

const winston = require('winston');

const alignedWithColorsAndTime = winston.format.combine(
  // winston.format.colorize({ all: true }),
  // winston.format.timestamp(),
  // winston.format.align(),
  winston.format.printf((info) => {
    const { level, message, ...args } = info;

    if (level.indexOf('error') !== -1) {
      console.log(typeof message, JSON.stringify(message));
    }

    return `[${level}]: ${message} ${Object.keys(args).length ? JSON.stringify(args, null, 2) : ''}`;
  })
);

const transports = [new winston.transports.Console({
  level: 'debug',
  format: alignedWithColorsAndTime
})];

const logger = winston.createLogger({
  level: 'debug',
  transports
});

try {
  logger.info('aaaaa');
  logger.debug('bbbb');
  logger.error('eeee');

  throw new Error('Scary error');
} catch (error) {
  logger.error(error);
}

What do you expect to happen instead?

At least actual error message logged. Ideally - with stack.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:7
  • Comments:34 (3 by maintainers)

github_iconTop GitHub Comments

27reactions
gtseccommented, Oct 28, 2018

Had same issue, I had to implement logger.error myself:

  logger.error = err => {
      if (err instanceof Error) {
        logger.log({ level: 'error', message: `${err.stack || err}` });
      } else {
        logger.log({ level: 'error', message: err });
      }
    };
22reactions
jeremy-j-acksocommented, Jan 28, 2019

Suggesting this be closed due to #1576.

Can use the formatters.

const winston = require('winston');
const { format } = winston;

const logger = winston.createLogger({
  format: format.combine(
    format.errors({ stack: true }),
    format.metadata(),
    format.json(),
  ),
  transports: [ new winston.transports.Console() ],
});

logger.error(new Error('FakeError'));
Read more comments on GitHub >

github_iconTop Results From Across the Web

Winston not displaying error details - node.js - Stack Overflow
A quick and dirty way would be to log err.stack : logger.error ...
Read more >
How to Log Exception Properly - Medium
The problem with this is developer would not know that an error has happened. Common Mistake 2: Only log developer's message. log.error(" ...
Read more >
Access and Error Logs - The Ultimate Guide To Logging - Loggly
Access and Error Logs. Log Files. An Apache log is a record of the events that have occurred on your Apache web server....
Read more >
Designing Error Messages and a Logging Strategy in Node.js
Learn how to structure helpful error messages and follow a good logging strategy.
Read more >
Sitecore.Diagnostics.Log.Error() method is not working Inside ...
Actually, recommended practice would be to use a Namespace as your logger, as opposed to new object() or anything else.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found