clux / logule

A multi-transport, peer-dependent logging library for nodejs - UNMAINTAINED
MIT License
35 stars 5 forks source link

Feature: An optionally more compact json representation #6

Open vjpr opened 12 years ago

vjpr commented 12 years ago

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 commented 12 years ago

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 commented 12 years ago

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 commented 12 years ago

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

clux commented 12 years ago

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 commented 11 years ago

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