4 ms·
I personally prefer console.log('delay is ' + chalk.green(delay))); as it is more consistent with non console.log expressions. For example, just with copy/pas
by olalonde 12y ago
I personally prefer
console.log('delay is ' + chalk.green(delay)));
as it is more consistent with non console.log expressions. For example, just with copy/paste I could replace the above with:
var msg = 'delay is ' + chalk.green(delay);
console.log(msg);
But it's really just a matter of preference as long as only one convention is used throughout the same codebase.
- whichdan 12y agoUsing commas is a little bit nicer if you tend to log objects frequently.
- Arnavion 12y agoES6 template strings give you the best of both worlds. console.log(`delay is ${ chalk.green(delay) }`);
- deleted 12y ago[deleted]
- konstruktor 12y agoThis expression is always evaluated, creating a new string object on the heap that has to be collected, whereas the format string one is only evaluated on demand (matching log level). That's why most logging frameworks go for the latter. I haven't seen benchmarks regarding the performance impact, though.
- nawitus 12y agoI don't use console.log when I can. It's better to roll your own logging function which adds timestamps and automatically stringifies the object (preferably using a stringify function which handles cyclic objects). The prolem with using console.log(object) is that in Node.js the object is not stringified and on browsers the debugger displays the current state of the object, not the state when it was printed. I found that out the hard way.. And when you're using your own logging function you can use a proper logging framework like winston to save the logs to a database and a file in addition to the console (we also send client-side logs to the backend using socket.io for easy debugging).