trentm/node-bunyan — pull request #102 by rlidwka (2013-10-08 23:23:18+00:00)
Title: added CLI formatter function to bunyan module

It should be just one small change as far as module interface is concerned.

I added a publicly available function `formatRecord` with a following interface:
```javascript
rec = {... bunyan log object ...}
opts = {color:false, outputMode:'long', jsonIndent:2}

bunyan.formatRecord = function formatRecord(rec, opts) {}
```

Implementation wasn't really that easy though. This function was in `bin/bunyan` and it needs to be available to `lib/bunyan.js` as well. So it needs to be moved to some common place.

Basically, I created a file `lib/common.js` (required by both `bin/bunyan` and `lib/bunyan.js`) and moved all functions/variables that used in both places to that new file, removing all the duplicated code along the way.

It directly fixes #84, and it makes #13 easy to do. With formatter function available to bunyan.js is should be trivial to add formatting when writing to stdout, though I didn't do it because it would be a big interface change.

All tests pass on node 0.10.x. Not sure if it means anything, because it's easy to slip bugs in such PR. So I intentionally split it to a lot of commits so you can see more easily where it's a cut-paste and where it's an actual change.

Note: formatRecord(rec, opts) messes with rec object (it's first argument), removing several keys, so it might be undesirable. Also, if rec isn't a valid record, function returns null (better to throw, but it'd be bad for performance).

That's how it works now:

```javascript
> console.log(require('bunyan').formatRecord(
{"name":"app","hostname":"srv","pid":8318,"test":123,"level":20,"msg":"test message","time":"2013-06-10T15:04:00.292Z","v":0}
));

[2013-06-10T15:04:00.292Z] DEBUG: app/8318 on srv: test message (test=123)

> console.log(require('bunyan').formatRecord(
{"name":"app","hostname":"srv","pid":8318,"test":123,"level":20,"msg":"test message","time":"2013-06-10T15:04:00.292Z","v":0},
{outputMode: 'short'}
));

15:04:00.292Z DEBUG app: test message (test=123)
```