Node.js Debugging

By  on  

Proper logging is of massive utility for web apps, both during development and after deployment.  What can sometimes be difficult is organizing both the code and output of logging, i.e. knowing where each log message is coming from.  I recently found debug, a Node.js utility for organized and optimized debugging.

Creating an instance of debug is simple and you can create multiple loggers per file:

// Create multiple instances of debug
// In theory these would serve two different purposes
var debuggerA = require('debug')('worker:a'),
    debuggerB = require('debug')('worker:b');

// Sample usages of the debugger
function work() {
  debuggerA('doing lots of uninteresting work');
  setTimeout(work, Math.random() * 1000);
}

work();

function workb() {
  debuggerB('doing some work');
  setTimeout(workb, Math.random() * 2000);
}

workb();

Node.js Debug

The namespace given to a debug instance as you must use an environment variable to signal which loggers should go to STDOUT when the script is run:

// Show all debugger messages prefixed "worker:_____"
DEBUG=worker:* node app.js

The environment variable strategy for signaling which instances should output is brilliant as you may want only certain types of messages logged in production vs. development.  Use namespaces wisely!

I was also able to use chalk to color messages as desired:

var chalk = require('chalk');

debuggerA(chalk.red.bold('OMG an awful error!'));

debug is one of those utilities that has a very simple purpose and accomplishes the task well.  Don't skimp when it comes to logging informative messages -- they'll help you during development and could be critical when auditing the app after a security incident!

Recent Features

  • By
    6 Things You Didn’t Know About Firefox OS

    Firefox OS is all over the tech news and for good reason:  Mozilla's finally given web developers the platform that they need to create apps the way they've been creating them for years -- with CSS, HTML, and JavaScript.  Firefox OS has been rapidly improving...

  • By
    Vibration API

    Many of the new APIs provided to us by browser vendors are more targeted toward the mobile user than the desktop user.  One of those simple APIs the Vibration API.  The Vibration API allows developers to direct the device, using JavaScript, to vibrate in...

Incredible Demos

  • By
    QuickBoxes for Dojo

    Adding to my mental portfolio is important to me. First came MooTools, then jQuery, and now Dojo. I speak often with Peter Higgins of Dojo fame and decided it was time to step into his world. I chose a simple but useful plugin...

  • By
    Create a CSS Flipping Animation

    CSS animations are a lot of fun; the beauty of them is that through many simple properties, you can create anything from an elegant fade in to a WTF-Pixar-would-be-proud effect. One CSS effect somewhere in between is the CSS flip effect, whereby there's...

Discussion

  1. Allain

    Thanks for making this known. I’m pretty sure debug logs to STDERR so that piping still works.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!