Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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

Awesome, this is just what I've been looking for.

Dynamite

thany commented Feb 28, 2015

Can it be done in Firebug?

wixo commented Feb 28, 2015

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

Owner

paulirish commented Feb 28, 2015

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

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 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 commented Feb 28, 2015

👍

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 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

thanks

IRus commented Aug 5, 2015

Any tips for Firefox users?

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