Created
July 4, 2014 06:49
-
-
Save sheedy/232708ebf985231588d2 to your computer and use it in GitHub Desktop.
Smarter console.log
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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