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

Formatters #16

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

Formatters #16

wants to merge 6 commits into from

Conversation

cpsubrian
Copy link
Contributor

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.

@tj
Copy link
Owner

tj commented Oct 26, 2012

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

@cpsubrian
Copy link
Contributor Author

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.

@cpsubrian
Copy link
Contributor Author

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.

@tj
Copy link
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

@tj
Copy link
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

@cpsubrian
Copy link
Contributor Author

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.

@tj
Copy link
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

@cpsubrian
Copy link
Contributor Author

I could see that. jsontools or something.

@cpsubrian
Copy link
Contributor Author

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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants