Skip to content

Instantly share code, notes, and snippets.

@paulirish
Last active April 2, 2024 20:18
Show Gist options
  • Save paulirish/c307a5a585ddbcc17242 to your computer and use it in GitHub Desktop.
Save paulirish/c307a5a585ddbcc17242 to your computer and use it in GitHub Desktop.
resolving the proper location and line number through a console.log wrapper

console.log wrap resolving for your wrapped console logs

I've heard this before:

What I really get frustrated by is that I cannot wrap console.* and preserve line numbers

We enabled this in Chrome DevTools via blackboxing a bit ago.

If you blackbox the script file the contains the console log wrapper, the script location shown in the console will be corrected to the original source file and line number. Click, and the full source is looking longingly into your eyes.

You can try it out here: http://plnkr.co/edit/3wg4u9HUGXfFH0U7MR7j

Blackbox the source file:

image

You can right-click the file in the editor, as well. Or blackbox via regex in Settings.

Logs will be resolved back to their call frame.

image

FWIW, Blackboxing does a lot. Blackboxing a file means:

  • Exceptions thrown from library code will not pause (if Pause on exceptions is enabled),
  • Stepping into/out/over bypasses the library code,
  • Event listener breakpoints don't break in library code,
  • The debugger will not pause on any breakpoints set in library code.

Read more about it over at https://developer.chrome.com/devtools/docs/blackboxing

Cheers and thanks crbug/249575

@msafi
Copy link

msafi commented Feb 28, 2015

That's great! Thanks so much! Is it also possible to filter out 3rd party libraries from the stack trace by blackboxing? :D

@jlukic
Copy link

jlukic commented Feb 28, 2015

Semantic UI uses a slightly different implementation for wrapping console calls that preserves line numbers and doesn't require blackboxing.

module.debug = function() {
  if(settings.debug) {
    // overwrite self
    module.debug = Function.prototype.bind.call(console.info, console, settings.name + ':');
    module.debug.apply(console, arguments);
  }
};

We also use a console.table to do grouped performance logging, console.groupCollapsed is epic.

log gif

@leonardopaiva
Copy link

thanks

@IRus
Copy link

IRus commented Aug 5, 2015

Any tips for Firefox users?

@zspitzer
Copy link

here's the FF bug about implementing this https://bugzilla.mozilla.org/show_bug.cgi?id=1060904

@zspitzer
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment