console.log() doesn't use objects' toString()

4,701 views
Skip to first unread message

Aseem Kishore

unread,
Jul 15, 2011, 7:56:04 PM7/15/11
to nod...@googlegroups.com
If someObj has a toString() method defined that's not the native one, it would be super convenient if console.log(someObj) used it. Right now, it always uses util.inspect() for objects.

Is the current way by design?

Aseem

Nathan Rajlich

unread,
Jul 15, 2011, 8:55:32 PM7/15/11
to nod...@googlegroups.com
You can set a special .inspect function on any object, and then `console.log()` and friends will use that function instead of the default `util.inspect()` logic.

--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Koichi Kobayashi

unread,
Jul 15, 2011, 10:00:00 PM7/15/11
to nod...@googlegroups.com
Hi,

Or, you can use '%s' format pattern.

$ node
> process.version
'v0.4.9'
> console.log(new Error('Hello, World!'));
{ stack: [Getter/Setter],
arguments: undefined,
type: undefined,
message: 'Hello, World!' }
> console.log('%s', new Error('Hello, World!'));
Error: Hello, World!


Since Node v0.5.1 (V8 3.4), each property of Error was changed to unenumerated.

$ node
> process.version
'v0.5.1'
> console.log(new Error('Hello, World!'));
{}

:-<


--
{
name: "Koichi Kobayashi",
mail: "koi...@improvement.jp",
blog: "http://d.hatena.ne.jp/koichik/",
twitter: "@koichik"
}

Aseem Kishore

unread,
Jul 15, 2011, 10:07:42 PM7/15/11
to nod...@googlegroups.com
Thanks both, good to know, but my original request still stands: you shouldn't have to define an extraneous, non-standard method on all your classes, and you shouldn't have to type more than just console.log.

Also, to the last point, this seems like a bug/weakness in util.inspect -- should be showing *all* properties, not just the enumerable ones. i.e. Instead of Object.keys, Object.getOwnPropertyNames():


I wrote a custom inspection function a while back that does this and also goes up prototype chains better:


The one weakness mine has it doesn't detect circular references, but I haven't had the time/inclination to update that, since I haven't run into it yet. =P

I would love to see some of these improvements make it into util.inspect() and console.log(). I should find the time to properly submit patches...

Aseem

Thomas Shinnick

unread,
Jul 16, 2011, 12:25:05 AM7/16/11
to nodejs
Have you tried setting the second argument to util.inspect() to true?

I have not appreciated the limitations of %j, but then I can
appreciate that if fancy is needed, I can use %s together with
util.inspect(). (My gripe is %j defaults to JSON.stringify which
doesn't handle circular references, though util.inspect() does just
fine.)

On Jul 15, 9:07 pm, Aseem Kishore <aseem.kish...@gmail.com> wrote:
[snip]
> Also, to the last point, this seems like a bug/weakness in util.inspect --
> should be showing *all* properties, not just the enumerable ones. i.e.
> Instead of Object.keys, Object.getOwnPropertyNames():
[snip]

Aseem Kishore

unread,
Jul 16, 2011, 2:15:20 AM7/16/11
to nod...@googlegroups.com
Yep. That second argument doesn't appear to make a difference in the examples I cite in node-dir's readme.

Cheers,
Aseem

Jorge

unread,
Jul 16, 2011, 5:12:37 AM7/16/11
to nod...@googlegroups.com
On 16/07/2011, at 04:07, Aseem Kishore wrote:

> (...)


> I would love to see some of these improvements make it into util.inspect() and console.log(). I should find the time to properly submit patches...

http://homepage.mac.com/jorgechamorro/sys.inspect.html
--
Jorge.

Isaac Schlueter

unread,
Jul 16, 2011, 5:32:16 AM7/16/11
to nod...@googlegroups.com
If you type `console.log({ toString: function () { return "foo" } })`
in a browser, it outputs it in the console in an inspectable format.
It does not print "foo" out to the console.

So that's what node does.

dodo

unread,
Jul 16, 2011, 12:37:37 PM7/16/11
to nod...@googlegroups.com
i'm using eyes.js [0] to have pretty printed objects:

a = {a:1}
require('util').inspect = require('eyes').inspector({stream:null})
console.log(a) // { a: 1 }

it also adds color :)

i forked it [1] and added hexy.js [2] , a nice hex dump printer,
to let eyes.js pretty print even buffers.

[0] https://github.com/cloudhead/eyes.js
[1] https://github.com/dodo/eyes.js
[2] https://github.com/a2800276/hexy.js

Reply all
Reply to author
Forward
0 new messages