Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log JavaScript objects and arrays in winston as console.log does?

I was looking at top Node logging systems: npmlog, log4js, bunyan and winston and decided to use winston for having the most npm monthly downloads.

What I want to set up is custom logger which I will be able to use on development environment with logger.debug(...) which won't log anything on production environment. This will help me so when I'm on development environment, I won't need to write anything since I'll see all the outputs.

This is what I have now:

var level = 'debug'; if (process.env.NODE_ENV !== 'development'){   level = 'production'; // this will never be logged! }  var logger = new winston.Logger({   transports: [     // some other loggings     new winston.transports.Console({       name: 'debug-console',       level: level,       prettyPrint: true,       handleExceptions: true,       json: false,       colorize: true     })    ],   exitOnError: false // don't crush no error }); 

Problem occurs when I'm trying to log JavaScript Object or Javascript Array. With Object, I need to do toJSON(), and for Array I need first JSON.stringify() and then JSON.parse().

It's not nice to write all the time this methods, just to log something that I want. Furthermore, it's not even resource-friendly, because those formatting methods need to be executed before logger.debug() realises that it's on production and that it shouldn't log it in the first place (basically, it's evaluating arguments before function call). I just like how old-fashined console.log() logs JavaScript objects and arrays.

Now, as I'm writing this question, I found that there is a way of describing custom format for every winston transports object. Is that the way of doing it, or is there some other way?

like image 234
Tommz Avatar asked Oct 15 '15 21:10

Tommz


People also ask

How do you log objects in Winston?

Winston should log your object with this set up. Show activity on this post. Show activity on this post. const winston = require("winston"); const { format, transports, createLogger } = winston; const path = require("path"); const consoleloggerLevel = process.

How do you log an object in console?

Answer: Use console. log() or JSON. stringify() Method You can use the console. log() method, if you simply wants to know what's inside an object for debugging purpose. This method will print the object in browser console.

Can you console log in node js?

js provides a console module which provides tons of very useful ways to interact with the command line. It is basically the same as the console object you find in the browser. The most basic and most used method is console. log() , which prints the string you pass to it to the console.

How do I view Winston logs?

Go to the example. log file in the logs folder to view the log. Winston allows you to implement multiple logging transports, i.e., a log can be recorded to a file, console, or database. The Logger configuration below logs to a console and a file.


2 Answers

logger.log("info", "Starting up with config %j", config); 

Winstons uses the built-in utils.format library. https://nodejs.org/dist/latest/docs/api/util.html#util_util_format_format_args

like image 124
Leo Avatar answered Oct 19 '22 16:10

Leo


try changing prettyPrint parameter to

prettyPrint: function ( object ){     return JSON.stringify(object); } 
like image 32
Bharat Kul Ratan Avatar answered Oct 19 '22 18:10

Bharat Kul Ratan