mcottondesign

Loving Open-Souce One Anonymous Function at a Time.

JavaScript: quick debugging tip

I have been doing a lot of in-browser testing. Because of this I want an insight to what the browser is doing.

console.log('DEBUG: ' + data)

But this gets unmanageable very quickly. The next evolution is to place a flag for what should be logged. It also helps to prefix all the logs with a category (DEBUG, INFO, ERROR, etc)

debug = true

if (debug) console.log('DEBUG: ' + data)

This is a little better but still isn't that much better. The next jump is to make debug into an object and get more specific about what I'm logging.

debug = {
    'info':         false,
    'startup':      true,
    'route':        true,
    'error':        true,
    'ajax':         false
}


if (debug.startup) console.log('STARTUP':  initializing navigation view)