2

Logging a function/class

or a flat object/array

results in logging a string representation (with no dropdown ► icon).

Logging complex objects (with own non-scalar properties) results in having hierarchical representation:

I would prefer to have the latter behaviour for all logged objects - both things that are typed in console and things that are logged with console.log.

For this reason only Firebug console output looks much more beneficial than Chrome dev tools.

I'm aware of console.dir, though it is inefficient to type console.dir(someVar) instead of someVar in console, and it is not appropriate to replace all console.log occurrences with console.dir in existing apps.

Other console methods (console.warn, console.error) have the same problem as console.log, but they don't have console.dir counterpart for them!

Can this Chrome dev tools behaviour be changed? Is there something that can help with this problem?

2
  • As a guess - you might override the normal console object and it's functions. Personally, I won't really consider this that big of a problem to throw more than few minutes of effort into it and that seems like too much to really bother with. It might be that there is a library that replaces the vanilla console but I've not really sought one out. Commented Aug 31, 2016 at 22:58
  • 2
    FYI: You don't need to type console.dir(someVar), you can just type dir(someVar) Commented Aug 31, 2016 at 22:59

1 Answer 1

4

I would generally just use dir(myObj) if you just want something quick. i don't think it's much effort to replace your log statements.

However, you could look into Custom Object Formatters in Chrome DevTools. You need to enable this feature in the DevTools settings and then implement the window.devtoolsFormatters object with the header and (optional) body you would like.

Depending on the type of object passed in, you can provide a different formatter for each. For example, you can check if the object is an array with Array.isArray(myObj) and then you can traverse the array properties and expose and format the ones you feel are relevant as the return object for the body.

You would need to run this as a snippet each time, or create a Chrome Extension to automatically inject this when you run your application.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.