logule icon indicating copy to clipboard operation
logule copied to clipboard

Feature: An optionally more compact json representation

Open vjpr opened this issue 13 years ago • 5 comments

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.

vjpr avatar Aug 17 '12 11:08 vjpr

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 avatar Aug 18 '12 14:08 clux

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 avatar Nov 11 '12 19:11 clux

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

clux avatar Nov 11 '12 19:11 clux

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 avatar Nov 11 '12 19:11 clux

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

clux avatar Nov 14 '12 09:11 clux