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

@thany
Copy link

thany commented Feb 28, 2015

Can it be done in Firebug?

@wixo
Copy link

wixo commented Feb 28, 2015

Nice! no more Error throwing :) https://github.com/wixo/debug

@paulirish
Copy link
Author

@wixo: check out http://www.briangrinstead.com/blog/console-log-helper-function .. it preserves original line number without throwing an exception.

@mahemoff
Copy link

Very nice and I was hoping to see some way to automatically whitelist the blackboxed URLs - the regex list didn't disappoint.

Suggestion for further improving it: Standardise the whitelist into a manifest.json equivalent (dev.json or something) so the default setting can be part of the project instead of (or in addition to) the browser settings. It would be a hassle for every developer of a project to have to configure the same whitelist manually after checkout and whenever it needs to change.

@patik
Copy link

patik commented Feb 28, 2015

@wixo also supports line numbers: https://github.com/patik/console.log-wrapper/ and logs in legacy browsers (but no line numbers there)

@derblub
Copy link

derblub commented Feb 28, 2015

👍

@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