whatwg / console

Console Standard
https://console.spec.whatwg.org/
Other
269 stars 67 forks source link

`console.once`: A console function that only logs once #175

Closed shshaw closed 2 years ago

shshaw commented 4 years ago

Feature Request: A console function that only logs once, regardless of how many times it is called.

Many times while debugging, I need to log some data that happens in a callback or loop that runs repeatedly ( onMouseMove, setInterval, requestAnimationFrame ). However, the browser can lock up or slow down when logging a ton of data, and trying to look at the data while the console fills up can be near impossible.

I'd love to see a special console function that will log only once for a label.

For example:

for (var i = 0; i < 2000; i++) {
  console.once(
    "my-function",
    "This only runs once, despite getting called repeatedly",
    i
  );
}

// Console output: "This only runs once, despite getting called repeatedly" 0

Really basic code mockup: https://codepen.io/shshaw/pen/NWGYzgv?editors=0011

This functionality can be done by the developer using a once style callback, but that requires extra code snippets or some kind of external dependency, like my code mockup. A built-in function would be very useful.

Twitter Thread: https://twitter.com/shshaw/status/1258781783996403715

domfarolino commented 4 years ago

I personally don't think this would be super useful, and a solution to make the developer experience better is probably best handled by the console embedder. The console interface defines definitions of methods that log, so I think if you want to limit how much you're logging, you should just call the method less, or create your own once as you've done.

Most browsers these days have some UX innovations that coalesce identical subsequent console logs to make it easier to visually parse the console log after copious logging, which I think is nice and solves one of the problems you're having.

Plus, when would the "once" be reset? Does it log only once per document that the console is associated with? Does a timer reset it? Should there be an API to reset it? Would some developers like to reset it after an SPA navigation, which we couldn't really detect in the spec? This metric probably varies depending the use-case, so I think for the best experience it makes sense to leave this out of the standard.

domfarolino commented 2 years ago

Hi, since there hasn't been much movement on this issue I'll go ahead and close it. Sorry, thanks for filing.