It might be totally impractical to do this properly, so I'll understand if it's immediately closed as wontfix, but a) it'd be really useful, and b) @domenic sent me here!
Basically, it'd be really great if there was something vaguely equivalent to console.group
, as it's incredibly useful for debugging in the browser. I whipped up node-console-group which is a very naive implementation (can't handle wrapped lines, etc) but good enough for my current needs - does this seem like something that would be worth fleshing out and adding to io.js?
This looks to be implemented in all major browsers, so +1 for parity. I think your example is a bit _too_ fancy on whitespace, and we should probably only handle the indentation (2 chars?) and maybe add a character on the first char, like |
.
What are use-cases for it?
I see this method for the first time honestly.
This looks like it can be done in userland just fine? Is there any thing we need to better support it? Otherwise I'm -1.
It can be done in userland, but what about compatibilty with scripts using these console methods? They'd unnecessarily error out.
I think I'm +1 on the general idea, since we already have most other console.*
functions that browsers provide.
However, I'm not sure about the proposed formatting. Perhaps there is some other layout that might scale better with nesting? Maybe we could (additionally) support some sort of level
parameter like util.inspect()
has?
I am +1, because we are better to implement same api on browser as possible.
BUT, console
is a PANDORA box .....
DeveloperToolsWG members are trying to standardize console
API.
According to the doc
console.clear
console.count
console.debug
console.dirxml
console.table
console.group / groupCollapsed / groupEnd
console.isIndependentlyComposed
console.profile/profileEnd
console.timeline/timelineEnd
console.timeStamp
| Specifier | Description | node/io.js implement status |
| :-- | :-- | :-- |
| %s
| Formats the value as a string (cooercing via toString() if necessary) | same |
| %d
, %i
| Formats the value as an integer | Formats the value as a number (not integer and %i is not implemented) |
| %f
| Formats the value as a floating point value | not implemented but %d is implemented |
| %o
| Formats the value as an expandable DOM Element (or JavaScript Object if it is not) | not implemented |
| %O
| Formats the value as an expandable JavaScript Object | not implemented but %j is implemented |
| %c
| Formats the output string according to CSS styles you provide | not implemented |
We need to define what API should be implemented / should not be implemented.
And if we implement the API, we should follow the standardize API specification.
What are use-cases for it?
It makes logging output much more legible, especially when you have a lot of it. It's particularly useful when you have a function that gets called frequently, and you want to understand how (or if) it's being called from a particular point in your code. It's also very useful for understanding anything that happens recursively, because there's visual structure involved.
My words aren't really doing it justice, but it's a bit like going from alert()
to console.log()
- you can just debug a lot more efficiently.
I'm not sure about the proposed formatting
That's fair - in fact I'm not really proposing it, as such, it's just what I cobbled together this afternoon. I'm certain it can be improved.
Thanks for considering this. I totally understand the preference for userland solutions, though as @Fishrock123 notes it does necessitate monkey-patching.
:+1: just for the parity with browsers. As stated, there does not seem to be official standard for console
API, but these nearly match:
I think Node should implement something code-compatible with these and then later move to standardized console
API if there ever is going to be one.
Even Microsoft IE/Chakra JS has now console.group
, console.groupCollapsed
, console.groupEnd
:
This should be quite easy to implement. The general consensus seems to be in favor.
console.group
should add one level of indentationconsole.groupEnd
should remove one level of indentationconsole.groupCollapsed
should be console.group
As for styling, I'd suggest just two spaces as a start.
@silverwind Actually console.groupCollapsed
should be same as console.group()
. It is expected to be followed by console.groupEnd()
too.
console.groupCollapsed()
Creates a new logging group that is initially collapsed instead of open, as with console.group().
Right, misinterpreted that. Should be aliased to console.group
.
This should be quite easy to implement. The general consensus seems to be in favor.
- console.group should add one level of indentation
- console.groupEnd should remove one level of indentation
- console.groupCollapsed should be a noop in our case aliased to console.group
console.groupCollapsed
should be implemented as real function. In this case external gui debuggers can handle it correctly. For example node-inspector
based on DevTools.
@3y3 would something like this work for node-inspector
?
console.groupCollapsed = function () {
console.group.apply(console, arguments);
}
@silverwind , yes, this would work.
What about indents node would add to the output? node-inspector
removes them and just marks the console message to a specific group/level?
node-inspector removes them and just marks the console message to a specific group/level?
node-inspector will remove them then (if) this feature will be implemented.
3y3 commented a minute ago
node-inspector removes them and just marks the console message to a specific group/level?
node-inspector will remove them then (if) this feature will be implemented.
I correct myself. Node Inspector will receive not formatted messages, in other words, he doesn't need to trim leading spaces.
Node Inspector will receive not formatted messages
Oh, I too forgot that node-inspector
wraps the entire function call. No issues then.
I've opened a pull-request #1727 to implement this feature in core lib
Closing this one. This has come up several times and we keep landing on not pursuing this.
Why do you keep landing in not pursuing this? What are the arguments?
I too think these are not off the table yet. if we could get these group methods implemented in a way so they work with --inspect
(e.g. same nested output as when used directly in the browser), they would add some real value.
This is fixed, re-open if anything needs changing.
@bmeck Do you have a link to a PR/commit? Can't find it (and it's not in 7.1.0). https://github.com/nodejs/node/search?q=%22console.group%22
And console.js
has no mention of it: https://github.com/nodejs/node/blob/master/lib/console.js
@SimenB oh interesting, it appears when --inspect
is enabled (when I was checking) you get an entirely different console
global. That seems like odd behavior / should not be the case.
It's probably Chrome's console
then? Yeah, that would be unexpected
@SimenB even in node's repl it is replaced, not chrome's, https://github.com/nodejs/node/blob/8f00455c5194154ce909ddac6488ae2e42976a4c/deps/v8_inspector/src/inspector/v8-console.cc#L656 replaces what is in console.js
Implemented but it doesn't seem to actually do anything?
$ node -v
v8.1.4
$ node -e "console.log(console.group.toString())"
function group() { [native code] }
$ cat test.js
console.log("This is the outer level");
console.group();
console.log("Level 2");
console.group();
console.log("Level 3");
console.warn("More of level 3");
console.groupEnd();
console.log("Back to level 2");
console.groupEnd();
console.log("Back to the outer level");
$ node test.js
This is the outer level
Level 2
Level 3
More of level 3
Back to level 2
Back to the outer level
$
I would expect the groups to at least be indented.
Interesting, looks like a recent v8 update defined a number console
methods that are available in Chrome, but because they aren't implemented in Node.js, they seem to be just noops. Maybe we should undefine them when not launched with --inspect
so to not confuse users?
Maybe we should undefine them when not launched with
--inspect
so to not confuse users?
We cannot do that easily because we support opening inspector during runtime (https://nodejs.org/api/inspector.html#inspector_inspector_open_port_host_wait).
We should just implement group
, groupCollapse
, and groupEnd
with indentation. It's not that difficult.
https://github.com/nodejs/node/pull/14910 PR for the most minimal console.group()
and console.groupEnd()
implementation I could muster.
Most helpful comment
I too think these are not off the table yet. if we could get these group methods implemented in a way so they work with
--inspect
(e.g. same nested output as when used directly in the browser), they would add some real value.