Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added CLI formatter function to bunyan module #102

Open
wants to merge 11 commits into
base: master
Choose a base branch
from

Conversation

rlidwka
Copy link

@rlidwka rlidwka commented Oct 8, 2013

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)

@qubyte
Copy link

qubyte commented Dec 8, 2013

What's the status of this?

@qubyte
Copy link

qubyte commented Mar 26, 2014

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.

@rlidwka
Copy link
Author

rlidwka commented Mar 27, 2014

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.

@qubyte
Copy link

qubyte commented Mar 27, 2014

Yeah, that's what I currently do. It just doesn't seem very DRY.

@felipap
Copy link

felipap commented Aug 27, 2014

What's the status of this?²

@jamesplease
Copy link

Just chiming in here to say that I wish this were a thing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Question: Is the bunyan CLI formatter available as a module?
5 participants