Created
August 18, 2014 14:41
-
-
Save designbyadrian/2eb329c853516cef618a to your computer and use it in GitHub Desktop.
Hijack console.log, console.warn, and console.error without breaking the default browser function.
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
var cl,ce,cw; | |
if(window.console && console.log){ | |
cl = console.log; | |
console.log = function(){ | |
MyLogFunction(arguments); | |
cl.apply(this, arguments) | |
} | |
} | |
if(window.console && console.warn){ | |
cw = console.warn; | |
console.warn = function(){ | |
MyWarnFunction(arguments); | |
cw.apply(this, arguments) | |
} | |
} | |
if(window.console && console.error){ | |
ce = console.error; | |
console.error = function(){ | |
MyErrorFunction(arguments); | |
ce.apply(this, arguments) | |
} | |
} |
if (window.console && console) {
for (let c in console) {
if (typeof console[c] === 'function') {
const cx = console[c]
console[c] = function () {
const betterArgs = massageArguments(arguments)
cx.apply(this, [...betterArgs])
}
}
}
}
Same as above just easier copy and paste.
What would the massageArguments look like? I'm trying to apply formatting to the console output.
The massageArguments
method will get passed the arguments object and from there you can apply your formatting to each of the arguments that were originally passed to the console.<method>()
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
awesome! Thanks for this
I tweaked and did something like this in ES6
`if (window.console && console) {