Managing Debug Messages Console.log

My JS code is usually populated with debug console.log() messages. Sometimes it’s better to disable them or disable some of them.

I can, for example, console.log() statement in some function with conditions defined by some constants. Is this a better way to manage debugging results or more elegant alternatives?

+7
source share
4 answers

Wrapping console.log into a function works well. But note that there are also many registration utilities for javascript. A bit of google on "js logger" may produce suitable results.

+4
source

Bunyan registration module is popular for node.js

Sample hi.js code:

 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} 

Then you can filter from the command line:

 $ node hi.js | bunyan -l warn [2013-01-04T19:08:37.182Z] WARN: myapp/40353 on localhost: au revoir (lang=fr) 
+5
source

If you are using Node.js, then debug is extremely efficient as an alternative to console.log ()

This will basically replace console.log (), except that you can enable it on the command line with the DEBUG environment variable based on how you initialized it in each file.

Let's say I have a project with several files specified in the 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 initialized debugging with the name one-one in the first file and one-two in the second file.

On the command line, I can run them as follows:

 node index.js 

Result: no debug output. However, if I run it as follows:

 DEBUG=* node index.js 

Both debug statements will be recorded, however, in different colors and with the name of the debug (one-one or one-two), so I can indicate which file they came from.

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

DEBUG = * - two node index.js

To get the debug result that was set with "-two" at the end of the name or

DEBUG = one - * node index.js

so that it all starts with the “one” -

You can also say that you want everything, or a lot of things, or exclude templates or sets. To exclude something that is preceded by a dash, an example:

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

This will include everything from “one” or “monkey” and exclude everything called “monkey: banana” or “elephant”, or start with “chimpanzee:”

If you want to exclude everything except the following:

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

+3
source

JS logger is a good and easy tool with smooth settings for log message levels and several predefined logging levels (DEBUG, INFO, WARN, ERROR).

+2
source

All Articles