zavg
zavg

Reputation: 11061

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?

Upvotes: 7

Views: 1512

Answers (4)

Guy
Guy

Reputation: 67230

If you're using Node.js then debug is extremely effective as an alternative to console.log()

It's basically a substitute for console.log() except you can enable it at the command line with the DEBUG environment variable based on how you've initialized it in each file.

Let's say I have a project with a couple of files referenced from my index.js file:

one.js

var debug = require('debug')('one-one');

var func = function() {
  debug('func');
}

two.js

var debug = require('debug')('one-two');

var func = function() {
  debug('func');
}

You've initialized debug with the name "one-one" in the first file and "one-two" in the second file.

On the command line I can run them like this:

node index.js

Result: no debug output. However, if I run it like this:

DEBUG=* node index.js

The both the debug statements will get written out, however, in different colors and with the debug name (one-one or one-two) so I can tell which file they came from.

Now let's say you want to narrow it down a bit more. You could run:

DEBUG=*-two node index.js

To only get output from debug that's been set with "-two" at the end of the name or

DEBUG=one-* node index.js

to get everything starting with "one-"

You can also say that you want everything, or a set of things, or exclude patterns or sets. To exclude something you precede it with a dash, an example:

DEBUG=one*,monkey*,-monkey:banana,-elephant,-chimp:* node index.js

This will include everything starting with "one" or "monkey" and exclude anything called "monkey:banana", or "elephant" or starting with "chimp:"

If you wanted to exclude everything except then:

DEBUG=*,-pattern1,-pattern2 node index.js

Upvotes: 3

250R
250R

Reputation: 37131

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)

Upvotes: 5

zavg
zavg

Reputation: 11061

JS logger is quite good and lightweight tool with flixible settings for log messages levels and several predefined logging levels (DEBUG, INFO, WARN, ERROR).

Upvotes: 2

chtenb
chtenb

Reputation: 16184

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.

Upvotes: 3

Related Questions