Chrome Logger for Node.js

24 April 2013

Chrome Logger is a Chrome extension that allows you to display your server side debugging messages in the Chrome console.

So, I made a simple implementation for Node.js to help you to debug your Node.js application directly in Chrome.

You can found node-chromelogger on GitHub.

Juste make a

npm install chromelogger

like any other npm package.

Then, use it in your application:

var chromelogger = require('chromelogger');
var http = require('http');

var server = http.createServer();

server.on('request', chromelogger.middleware);

server.on('request', function(req, res) {
  res.chrome.log('Message from Node.js %s', process.version);
  res.end('Hello World');
});

server.listen(7357);

Node Chrome Logger provide several logging methods on the ServerResponse (res) object:

  • res.chrome.log
  • res.chrome.warn
  • res.chrome.error
  • res.chrome.info
  • res.chrome.group
  • res.chrome.groupEnd
  • res.chrome.groupCollapse

These methods matches the Console API of the Chrome Developer Tools.

You can also use it as an ExpressJS middleware if you want (see Readme for more informations).

The main limitation is it uses the HTTP Headers to send the informations to the client, so if you started to send the body you can't send more debug messages to the client and it will trigger an error.

If you need a more advanced Node.js debug tool I suggest you to use the great Node Inspector package.

Want to contribute to this article? Fork it on GitHub