Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Feature: An optionally more compact json representation #6

Open
vjpr opened this Issue · 5 comments

2 participants

@vjpr

Would be nice to have the ability to override how an object is printed too.

Sometimes I prefer this:

method=events.batchProcess id=883 params=[object Object] ...

to this:

{ method: events.batchP....
    params: { // etc }
}

The Winston logging library prints objects in the first way by default.

@clux
Owner

Yeah, I can see this being nice.

Do you know how does winston does this? I couldn't get it to work with the recent version and they say they just use util.inspect, which essentially is equivalent to what logule does.

@clux
Owner

This sort of works for big circular objects by passing them in separately to the % identifiers at the moment:

var log = require('logule').init(module);
var b = {a:1};
b.b = b; // make it circular

log.info(b);
// prints: 19:15:17 - INFO  - { a: 1, b: [Circular] }

log.info("%j", b) // tries to JSON.stringify a circular: throws

But rereading this, ideally what you want is some sort of customized one-level deep loop like this:

var neatObjectify = function (o) {
  return Object.keys(o).map(function (k) {
    return k + '=' + o[k].toString();
  }).join(' ');
};

perhaps also mapping functions to function.name when it exists.

It would be really nice if this was discussed properly with the node team that did util.js, because otherwise I pretty much have to fork util.format to get an extra identifier in there I think. Not saying this is an impossibility though, but I probably won't push it to the top of my priorities list for a little while at least. I still like the idea though - so feel free to do it.

(Btw sorry you probably got a random response 5 minutes ago that I did not think through and removed)

@clux
Owner

Actually, what you want is util.inspect(obj, false, 0) i.e. depth parameter set to. May be easier than I thought.

@clux
Owner

Yeah, I think I'll do this now actually. Turns out I can even get repl colors on the objects, just by rewriting util.format a little and calling util.inspect with the right parameters.

I'm thinking %0 for a depth zero object scan and %1 for one level one etc (sometimes zero depth is bad if you have small objects/arrays).

@clux
Owner

Left it out of 2.0 for reasons described in commit there. Might try to bug node core about it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.