Skip to content

Instantly share code, notes, and snippets.

@sheedy
Created July 4, 2014 06:49
Show Gist options
  • Save sheedy/232708ebf985231588d2 to your computer and use it in GitHub Desktop.
Save sheedy/232708ebf985231588d2 to your computer and use it in GitHub Desktop.
Smarter console.log
// Source: http://remysharp.com/2014/05/23/where-is-that-console-log/
//
// If you include this as high as possible in your code base, all subsequent console.log (or warn) calls will include the line the call was made from
['log', 'warn'].forEach(function(method) {
var old = console[method];
console[method] = function() {
var stack = (new Error()).stack.split(/\n/);
// Chrome includes a single "Error" line, FF doesn't.
if (stack[0].indexOf('Error') === 0) {
stack = stack.slice(1);
}
var args = [].slice.apply(arguments).concat([stack[1].trim()]);
return old.apply(console, args);
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment