Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Console.log debug messages managing

My JS code is usually full of console.log() debug messages. Sometimes it is better to turn them off, or to turn off some part of them.

I can, for example, wrap console.log() statement in some function with conditions which are defined by some constants. Is it the best way to manage debug output or are more elegant alternatives?

like image 281
zavg Avatar asked May 11 '13 15:05

zavg


People also ask

How do I check my console debugger?

The console. debug() method outputs a message to the web console at the "debug" log level. The message is only displayed to the user if the console is configured to display debug output. In most cases, the log level is configured within the console UI.

How do you debug messages?

It turns out that you can enable debug menu on Google Messages by entering *xyzzy* to the search field. It will add two additional items to the settings menu and a bunch of options to the dropdown menu.

What is difference between console log and console debug?

They are almost identical - the only difference is that debug messages are hidden by default in recent versions of Chrome (you have to set the log level to Verbose in the Devtools topbar while in console to see debug messages; log messages are visible by default).


2 Answers

Bunyan logging module is popular for node.js

Example code hi.js:

var bunyan = require('bunyan');
var log = bunyan.createLogger({name: 'myapp'});
log.info('hi');
log.warn({lang: 'fr'}, 'au revoir');

Output:

{"name":"myapp","hostname":"localhost","pid":40161,"level":30,"msg":"hi","time":"2013-01-    04T18:46:23.851Z","v":0}
{"name":"myapp","hostname":"localhost","pid":40161,"level":40,"lang":"fr","msg":"au revoir","time":"2013-01-04T18:46:23.853Z","v":0}

You can then filtering from command lines:

$ node hi.js | bunyan -l warn
[2013-01-04T19:08:37.182Z]  WARN: myapp/40353 on localhost: au revoir (lang=fr)
like image 62
250R Avatar answered Nov 09 '22 16:11

250R


Wrapping console.log into a function works well. But notice that there are also a lot of logging utilities out there for javascript. A little google on "js logger" may yield suitable results.

like image 38
chtenb Avatar answered Nov 09 '22 18:11

chtenb