node-bunyan
node-bunyan copied to clipboard
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:
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:
> 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)
What's the status of this?
Anything happening with this? It'd be nice to have access to pretty printing via the module. This would make developing with bunyan a lot nicer.
It'll probably need to be closed, since it's very big change, and it probably won't be merged cleanly.
I ended up writing my own log formatter, you might want to do the same.
Yeah, that's what I currently do. It just doesn't seem very DRY.
What's the status of this?²
Just chiming in here to say that I wish this were a thing!