Formatters #16

Open
wants to merge 6 commits into
from

Conversation

Projects
None yet
2 participants
Contributor

cpsubrian commented Oct 25, 2012

Introduces the concept of 'formatters' and ALL CLI output runs through a formatter.

A formatter is merely a commonjs module that exports an object. The object should follow the following signature:

module.exports = {
  // Required. Outputs lines.
  output: function(lines) {
   // console.log, or write to a file, or whatever.
  },

  // Optional. Map function.
  map: function(obj) {
    return obj.type;
  },

  // Optional. Select function.
  select: function(obj) {
    return obj.level === 'error';
  },

  // Optional. Reduce function.
  reduce: function(lines) {
      // return reduced lines.
  }
};

Formatters can live in three possible locations:

  • lib/formatters
  • $HOME/.jog/formatters
  • $CWD/.jog/formatters

This allows someone to maintain formatters that they like to use on either a per-machine, or per-project basis. Awesome! Also, the formatters are loaded in that order, so project formatters can override machine ones, or core ones.

Four core formatters are included:

  • json
  • color
  • short
  • default

You use the formatters via the --output command-line option.

jog -F /tmp/logs.log -f --output short

jog -F /tmp/logs.log -f -o short

-j --json is aliased to --output json

-c --color is aliased to --output color

I think this is a really flexible approach. For example you could create a formatter that saves images, or a formatter that renders a CLI bar chart, or whatever.

Owner

tj commented Oct 26, 2012

IMO this should be a concern for custom / other tools / shell scripts

Contributor

cpsubrian commented Oct 26, 2012

Care to elaborate? The idea is that its simply a way to 'save' jog commands with whatever filtering/formatting that you want. IMO it makes jog way more 'viable' for constant use during development and production.

If this sort of formatting is part of a separate CLI, then whats the point of jog (for my use case). I would probably just create my own jog with customizable formatting built in, but thats the type of module fragmentation that I though its better to avoid.

Contributor

cpsubrian commented Oct 26, 2012

I mean it would be possible to create a module that just prints json objects such that you would do:

jog -F /tmp/app.log -f --json | jsonprint --format short

But that doesn't let you reuse select or reduce functions at all. So then you have a tool that just wraps the entirety of jog which seems specific and redundant when, IMO, this formatters approach makes jog itself more extendable while adding minimal code.

Owner

tj commented Oct 26, 2012

i dont dislike the idea, i just dont want to bloat jog itself, i also want a repl and a query language but that's not going to be built within jog itself. ill take a closer look a the implementation and consider it

Owner

tj commented Oct 26, 2012

i dont know, personally I would still just write a little shell script or alias called say ./total-response-time with jog --file cloudup.log --type response --map duration --reduce "$ + _" etc

Contributor

cpsubrian commented Oct 27, 2012

I guess we have to agree to disagree :) I'll put some more thought into how I can do what I want cleanly without having to fork jog.

Owner

tj commented Oct 27, 2012

sounds good. i would even prefer cat some.log | map duration | reduce "$ + _" etc haha, but it would be lots of things to install, perhaps they should all be in one repo but separate executables

Contributor

cpsubrian commented Oct 27, 2012

I could see that. jsontools or something.

Contributor

cpsubrian commented Nov 1, 2012

I 'scratched my own itch' and created https://github.com/cpsubrian/node-joli which satisfies my needs. So, unless you think some version of this will be included in jog feel free to close this.

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