-
Notifications
You must be signed in to change notification settings - Fork 29.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
console.log and util.format formatting specifiers #10292
Comments
Not sure if aliasing
I'd propose the following steps to fix:
|
As mentioned in #1716 (comment) we don't implement the We could do |
I agree that there is no point in chasing a "spec" that doesn't exist, but this is a case where the major browser vendors agree! As of 2016 Dec 15 this is the current conversion specifier support:
IMHO nodejs should support %d %i %s %f This table is based on the following browser vendor resources:
EDIT: originally supported following browsers use of |
Well, there is definitely a "spec" but it's not really official to the language by any means: https://github.com/DeveloperToolsWG/console-object/ |
The browsers actually do something wholly unexpected with the string conversions: console.log("%s", {toString:function() { return "foo"; }}) My expectation is that "%s" would use the appropriate toString method, so that the output would be "foo". nodejs actually does what I would expect. Oddly, Chrome and Safari print "Object" rather than calling the object's toString. Since I think node is actually doing the right thing here, I will close now and try to bring this discussion to the relevant browser vendors before revisiting in node |
Let's leave this open, there's certainly room for improvement on our side, and I like you nice table :) |
@silverwind The confounding factor related to the trailing digits in the floating format is support for the printf width and precision fields. Firefox has limited support but Chrome completely ignores those fields. If you want to see a difference in FF, try Since there are NPM modules like printj that fully replicate the POSIX printf spec, it seems fair to argue that node core should only support basic substitutions and people should fall back to userland code for heavy lifting. @robertkowalski @terinjokes since you are mentioned in the whatwg spec, maybe you can chime in. |
@jsinmotion thanks. I too think we should start at basic support for |
@Fishrock123 While not an official part of the language, browsers have been pretty keen on working with the WHATWG console standard. We'd love it if Node.js can support what it can, and perhaps provide feedback on what it can't. @jsinmotion At this point, I don't think there's been any discussion on supporting POSIX (or Firefox)-like width/precision. If it's something you're really interested in, perhaps open a ticket for us to chat with interested parties. @silverwind I support adding |
@terinjokes Is there a good place to have active cross-collaboration? We are sort of stuck with some of the things we have, and work in a very different environment which renders some things such as |
I guess just at https://github.com/whatwg/console? Some of the issues seem kinda outdated there with relatively little discussion. |
@Fishrock123 Nope, that's the place. It's more active than it looks. Compared to Node.js, probably significantly less active, but there's also a much smaller group of people desiring to work on it. 😄 |
@Fishrock123 Please note that "differentiated
If you can differentiate, then awesome, but if you can't, then do something that makes sense. As for
|
I'd certainly welcome us following a spec, but there seems to be some opposition within the node collaborators to get mildly breaking changes like Can we get a decision from @nodejs/ctc on whether we can follow this spec? |
@terinjokes I think the spec needs some clarifications first: for example, the spec does not define format specifier so it is unclear whether implementations should handle cases like
|
I'm willing to work with a spec, but only if it's for us too, we need to be able to work on and influence it like a browser can. Attempting to conform to it without that is just putting ourselves into a hole haha. :) I have hopes, it seems positive, but I also have past reservations. |
@Fishrock123 I understand, and I'm glad to have your feedback. See you over there! |
@jsinmotion While I understand your frustration (or perhaps a disappointment) with how implementers handled console.assert, I don't understand why you aren't willing to work with me on other mutual problems. I would have been extremely happy to get the feedback of the format specifiers you've given above. As more and more developers handle both sides of the stack, and as one that has to do so, I'd expect that they'll prefer closer convergence, rather than the two objects drifting farther away. |
@terinjokes the real problem, as evidenced in that thread and the response, is logical:
That is a logical problem whereby the numbers certainly point to a resolution in which node copies bugs from browsers. And that seems silly and I fervently disagree with that idea. The right thing is to push the browsers to fix their bugs. I liked the particular example of console.assert abecause no one in the browser development teams actually thought about consulting the Firebug documentation when ripping the bug. And if repeating the bug is the recommendation it's better to forge on and do what is sensible rather than going down the rabbit hole of repeating browser bugs. @silverwind I originally commented that I supported implementing something paralleling the browser's %o %O for objects. This clashes with printf and as a result I am -1 for %o %O but still +1 to %d %i %f behavior aligning with printf |
console.log() in browsers is a debug tool for sticking messages in debug window that is viewable ONLY by developers. console.log() in node is a way of printing to the terminal, the actual user, or to whever the stdout has been directed. The fact that you can write some code that will call console.log in the browser and in the terminal doesn't mean that the code will have anything even vaguely like the same behaviour - node completely lacks a dev tools-like debug tool. Are you personally writing code that uses console, and find their behaviours to be meaningfully equivalent? What kind of code? |
For me it is useful the chrome inspector attached to nodejs, to control the flow of the code, execute functions and fix problems. I think you are missing things of usability, for example when there is an execution error, the console fails, together with node and does not recover, this should auto-load the inspector, something similar to what node-inspector does. |
could you elaborate what you mean by this?
and by this? how would a remote node micro service instance "load the inspector" on an uncaught exception? |
@sam-github should you ever use console.log for printing data to stdout? The docs are confusing here: https://nodejs.org/dist/latest-v7.x/docs/api/console.html
I interpret that to mean console should only be used for debugging purposes and that process.stdout.write should always be favored for non-debugging output. |
@sam-github ok I'll be more specific, when I work the integration of the V8 Inspector for Node.js with the flag inspect, nodejs gives me a url in console to place in the browser and to be able to use the Chrome DevTools connected to Node.js, then when there is a code error in the code flow, nodejs is blocked showing an error in console and Chrome DevTools stop working just showing a console error, example of an intentional error: Docker command: node console:
Chrome DevTools: For a case of an error what I have to do is: restart nodejs, take the new url of the terminal and load it in the browser, this process tires a little, it would be very useful that nodejs will restart automatically, without disconnecting the web socket, starting again The flow, and if it has the flag debug-brk use it. Another case is: if you make a code change, restart nodejs and the Chrome console is reloaded with the same or a new web socket This with node-inspector + forever can be done, the problem with this library is that it is quite unstable, tends to break when changing node version or simply stops working for no apparent reason. |
@diegoug That doesn't have anything to do with console, AFAICT, it looks like you are thinking that its the printing to stdout that is causing node to die, when node has already (tried) to die, actually, and the stdout messages are just its attempt to tell you why. I don't use debuggers for node, but it looks like whichever one you are using lacks a feature, the ability to trap uncaught exceptions, and keep node alive enough for the debug protocol to be used to inspect its near-death state @jsinmotion That text is relatively recent compared to the existence of the console, introduced only in #4428, and I think you are reading too much into it, anyhow. Its not even particularly accurate. Node does not "provide a debugging console"... even if you use And anyway, those docs are in a state of disrepair. I'm going to open a bug on this. EDIT: #10617 |
@silverwind The |
This change brings formatting specifiers available in `util.format` and consequently, `console.*` closer to what is supported in all major browsers. There is a breaking change with the `%d` specifier which previously served a double purpose of formatting both integer and floats. With this change, it will format only as integer. - `%d` is being changed to format only as integer. - `%i` is introduced as an alias to `%d`. - `%f` is introduced to format floating point values. When updating code, all instances of `%d` format strings that were supplied with floats should be changed to the `%f` format string. Fixes: nodejs#10292
|
This change brings formatting specifiers available in `util.format` and consequently, `console.*` closer to what is supported in all major browsers. - `%i` is introduced to format integer values. - `%f` is introduced to format floating point values. Fixes: nodejs#10292 PR-URL: nodejs#10308 Reviewed-By: James M Snell <jasnell@gmail.com>
I'm going to close this and open a separate issue for |
This change brings formatting specifiers available in `util.format` and consequently, `console.*` closer to what is supported in all major browsers. - `%i` is introduced to format integer values. - `%f` is introduced to format floating point values. Fixes: #10292 PR-URL: #10308 Reviewed-By: James M Snell <jasnell@gmail.com>
The Chromium console supports format specifiers: https://developers.google.com/web/tools/chrome-devtools/console/console-write#string_substitution_and_formatting
In particular, both
%i
and%d
specify an integral argument. Node.js does not perform the conversion for%i
:I think the fix is really simple: the relevant code is at https://github.com/nodejs/node/blob/master/lib/util.js#L86-L117 . Just adding the
i
case should be enough:If it makes sense I can send a PR
The text was updated successfully, but these errors were encountered: