Home

Awesome

postal.diagnostics

Version 0.7.4

What is it?

postal.diagnostics is a plug-in for postal.js that enables a 'smart' wiretap on message bus to log all or a filtered subset of messages. You provide a DiagnosticsWireTap instance with a "writer" callback - enabling you to write the logged messages to the console (for example), or to the file system (node.js), DOM Element, websocket or nearly anything else that can be a "writer target".

Why would I use it?

Developing a browser or node.js application using a local message bus like postal.js can greatly benefit from design/debug-time console logging. The postal.diagnostics plugin gives you tight control over what gets logged out to your writer callback, so it's fairly simple to narrow down your logged output to the messages you want to see. Having good visibility into the messages being published assists you in seeing the components of your application interact, and helps resolve odd runtime edge cases.

Ok, great, so how do I use it?

Instantiating a DiagnosticsWireTap

// simple wire tap:
// serializes the envelope via JSON.stringify(envelope, null, 4)
// and writes it to console.log (it also gets a default name)
var wireTap = new postal.diagnostics.DiagnosticsWireTap();

// using AMD/require.js
define(['postal.diagnostics'], function(DiagnosticsWireTap) {
	var wireTap = new DiagnosticsWireTap({ name: "console" });
	// other stuff here.....
});

// node.js - postal needs to be passed to the function returned by the module
// this pattern is common to all postal add-ons in node.js, since they need to
// act on the same instance of postal.
var postal = require('postal');
var DiagnosticsWireTap = require('postal.diagnostics')( postal );

var wireTap = new DiagnosticsWireTap({name: "console"});

Using Filters

Filters are a simple way to constrain what envelopes get passed to the writer callback you provide to the DiagnosticsWireTap instance. A filter is (usually) an object literal that should align with the hierarchy of a postal message envelope - though you only implement the members on which you want to match. For example:

// Consider an envelope like this:
{
	channel: "SomeChannel",
	topic: "Some.Topic",
	data: {
		foo: "bar",
		bacon: "sizzle"
	}
}

// to match the above envelope on topic name only, your filter object would look like:
{ topic: "Some.Topic" }

// to match the envelope by testing if a member of the data property matched a specific value:
{ data: { bacon: "sizzle" } }

// to match the envelope by testing if a member of the data property matched a regex:
{ data: { bacon: /sizzl/ } }

As mentioned above, filters can be provided when you instantiate the wiretap, or added later by calling addFilter

Here's an example of instantiating a DiagnosticsWireTap with 3 filters:

// The filters below mean that ANY envelope that passes
// at least one of the filters will be passed to the writer callback
var wireTap = new DiagnosticsWireTap({
	name: "console",
	filters: [
		{ channel: "MyChannel" },
		{ data: { foo: /bar/ } },
		{ topic: "Some.Topic" }
	]
});

Build, Dependencies, etc.

License

postal.diagnostics is dual-licensed MIT & GPL - use whichever is appropriate for your project.