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.

In 3.0.0 I cannot log objects without using JSON.stringify

See original GitHub issue

I’m on version 3, but since I’ve upgraded the only way to log objects is by stringify first the object. But with errors is even worse because I cannot log the error at all, I need to use error.message or directly the console.

This is my configuration for the development environment:

`` const { createLogger, format, transports } = require(‘winston’); const { combine, timestamp, label, printf } = format;

const environment = process.env.NODE_ENV;
const timestampFormat = 'YYYY-MM-DD HH:mm:SS';

const loggerFormat = printf((info) => {
    return `[${info.label}] ${info.message}`;
});
logger = createLogger({
    format: combine(
        label({ label: 'immoliste' }),
        format.colorize({ all: true }),
        format.splat(),
        format.simple(),
        loggerFormat
    ),
    transports: [
        new transports.Console({
            level: 'debug',
            colorized: true
        })
    ]
});

Doing this:

logger.error('Error sending contact message:', JSON.stringify(err));

I got only:

[mylabel] Error sending contact message:

But is a pain in the ass having to stringify everything, and doind that for errors. What am I doing wrong?, because in previous versions it was straightforward.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:28
  • Comments:18

github_iconTop GitHub Comments

57reactions
cpurycommented, Nov 29, 2018

Seriously, this seems like a downgrade. With Winston 2, I could just replace all console.X with winston.X. Why suddenly the ugly format tags?

35reactions
smyth64commented, Jun 30, 2019

Couldn’t accept that I have tot use %o always and made this simple solution:

const prettyJson = format.printf(info => {
  if (info.message.constructor === Object) {
    info.message = JSON.stringify(info.message, null, 4)
  }
  return `${info.level}: ${info.message}`
})

const logger = createLogger({
  level: 'info',
  format: format.combine(
    format.colorize(),
    format.prettyPrint(),
    format.splat(),
    format.simple(),
    prettyJson,
  ),
  transports: [
    new transports.Console({})
  ],
})

So this logger…

  logger.info({ hi: 123 })

…transforms to this in the console

info: {
    "hi": 123
}

Hope I can help you guys 😄 Just awful if we always had two use “%o”

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to convert javascript object into json without using "JSON ...
Because there is no stringify method in the JSON object in the following link. console. log(JSON. stringify(obj));
Read more >
Beware of Using JSON.stringify() for Logging - Level Up Coding
Recently I was working on a legacy system built on AWS Lambda and Node.js. It uses the console object along with JSON.stringify() to...
Read more >
fast-json-stringify - npm
Start using fast-json-stringify in your project by running `npm i ... it is not present in the object to stringify, fast-json-stringify will ...
Read more >
ConvertFrom-Json (Microsoft.PowerShell.Utility)
The ConvertFrom-Json cmdlet converts a JavaScript Object Notation (JSON) formatted string to a custom PSObject or Hashtable object that has a property for...
Read more >
JSON.Stringify() does not work in mith 3.3.0.7801
Mirth Connect 4.2.0 is now available as an appliance update and on our GitHub page ... But it specifies that it cannot stringify...
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