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.

It looks like 3.0 is logging {} for Error objects

See original GitHub issue

Hi All I am having some hassle with winston 3.0 logging empty error objects when it is using a json format.

module.exports = winston.createLogger({
  format: format.combine(
    format.label({ label: 'my label'}),
    format.timestamp(),
    format.prettyPrint()
  ),
  transports: [
    new (winston.transports.Console)({
      level: error
    })
  ]
}); 

logs the expected

 {error: [stack trace here] }

while

module.exports = winston.createLogger({
  format: format.combine(
    format.label({ label: 'my label'}),
    format.timestamp(),
    format.json()
  ),
  transports: [
    new (winston.transports.Console)({
      level: error
    })
  ]
}); 

logs the erroneous

{error: {} } 

when it is passed

logger.log({
  error: new Error('my error message')
 });

Manually creating a format with

winston.format.printf

also returned the erroneous result.

Does anybody know of a workaround for this?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:17
  • Comments:13 (3 by maintainers)

github_iconTop GitHub Comments

39reactions
LukasBombachcommented, Sep 21, 2018

More concise version:

function format() {
  const replaceError = ({ label, level, message, stack }) => ({ label, level, message, stack });
  const replacer = (key, value) => value instanceof Error ? replaceError(value) : value;
  return combine(label({ label: 'ssr server log' }), format.json({ replacer }));
}

const logger = createLogger({
  format: format(),
});

But this is what I actually do to have pretty logs locally and JSON errors in production:

function prodFormat() {
  const replaceError = ({ label, level, message, stack }) => ({ label, level, message, stack });
  const replacer = (key, value) => value instanceof Error ? replaceError(value) : value;
  return combine(label({ label: 'ssr server log' }), format.json({ replacer }));
}

function devFormat() {
  const formatMessage = info => `${info.level} ${info.message}`;
  const formatError = info => `${info.level} ${info.message}\n\n${info.stack}\n`;
  const format = info => info instanceof Error ? formatError(info) : formatMessage(info);
  return combine(colorize(), printf(format))
}

const logger = createLogger({
  level: process.env.LOG_LEVEL || 'info',
  exitOnError: false,
  transports: [new transports.Console()],
  format: isProd ? prodFormat() : devFormat(),
});

which gives me this in production

bildschirmfoto 2018-09-21 um 16 46 35

and this in development

bildschirmfoto 2018-09-21 um 16 47 04

11reactions
dhensencommented, Mar 26, 2018

I’m running into this as well. Shouldn’t winston handle this out of the box? @indexzero what do you think about this?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Logging errors in Slim 3 - Rob Allen's DevNotes
In this post, I'm going to look at error handling. ... App\Handlers\Error object and then retrieve the Logger from the container as we...
Read more >
Log shows Error object: {"isTrusted":true} instead of actual ...
I have an event handler that looks like this ...
Read more >
LoggerExtensions.LogError Method - Microsoft Learn
Formats and writes an error log message. LogError(ILogger, EventId, Exception, String, Object[]). Formats and writes an error log message.
Read more >
logging — Logging facility for Python — Python 3.11.1 ...
Logs a message with level ERROR on this logger. The arguments are interpreted as for debug() . Exception info is added to the...
Read more >
Designing Error Messages and a Logging Strategy in Node.js
So let's see what a proper logging strategy looks like and how we can structure our error messages to be helpful.
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