Skip to content

Instantly share code, notes, and snippets.

@karlpokus
Last active July 6, 2016 08:40
Show Gist options
  • Save karlpokus/473de03f769f39796d44d3014c979719 to your computer and use it in GitHub Desktop.
Save karlpokus/473de03f769f39796d44d3014c979719 to your computer and use it in GitHub Desktop.
Mute and capture console.log in the browser
// 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']]
@karlpokus
Copy link
Author

Added fancy namespaced version. Demo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment