Last active
July 6, 2016 08:40
-
-
Save karlpokus/473de03f769f39796d44d3014c979719 to your computer and use it in GitHub Desktop.
Mute and capture console.log in the browser
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
// completely mute | |
console.log = function() {}; | |
// captured data in log | |
var log = []; | |
console.log = function() { | |
log.push([].slice.call(arguments)); | |
}; | |
// fancy namespaced version. Demo -> http://codepen.io/KarlPokus/pen/rLwXQX/ | |
(function(c) { | |
var saved = c.log, | |
data = []; | |
c.mute = function() { | |
c.log = function(str) { | |
var args = (arguments.length > 1) ? [].slice.call(arguments) : str; | |
data.push(args); | |
}; | |
}; | |
c.resume = function() { | |
c.log = saved; | |
var out = data; | |
data = []; // reset | |
return out; | |
} | |
})(console); | |
// test | |
console.log('a'); // logs | |
console.mute(); | |
console.log('b'); // muted | |
console.log('c', 'd'); // muted | |
var muted = console.resume(); // reset and resume | |
console.log('e'); // logs | |
console.log(muted); // logs ['b', ['c', 'd']] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Added fancy namespaced version. Demo