Created
January 27, 2017 16:34
-
-
Save receptor/b6526e81f71c7393e534df0e1b7a6e49 to your computer and use it in GitHub Desktop.
console.log wrapper
This file contains 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
// Full version of `log` that: | |
// * Prevents errors on console methods when no console present. | |
// * Exposes a global 'log' function that preserves line numbering and formatting. | |
(function () { | |
var method; | |
var noop = function () { }; | |
var methods = [ | |
'assert', 'clear', 'count', 'debug', 'dir', 'dirxml', 'error', | |
'exception', 'group', 'groupCollapsed', 'groupEnd', 'info', 'log', | |
'markTimeline', 'profile', 'profileEnd', 'table', 'time', 'timeEnd', | |
'timeStamp', 'trace', 'warn' | |
]; | |
var length = methods.length; | |
var console = (window.console = window.console || {}); | |
while (length--) { | |
method = methods[length]; | |
// Only stub undefined methods. | |
if (!console[method]) { | |
console[method] = noop; | |
} | |
} | |
if (Function.prototype.bind) { | |
window.log = Function.prototype.bind.call(console.log, console); | |
} | |
else { | |
window.log = function() { | |
Function.prototype.apply.call(console.log, console, arguments); | |
}; | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment