Skip to content

Instantly share code, notes, and snippets.

@sehrgut
Created June 24, 2015 20:01
Show Gist options
  • Save sehrgut/db5532a40cd4b47b93d1 to your computer and use it in GitHub Desktop.
Save sehrgut/db5532a40cd4b47b93d1 to your computer and use it in GitHub Desktop.
console.log() with message history
console.log = (function (oldlog) {
var console_log = oldlog.bind(console);
var messages = [];
var newlog = function (msg) {
messages.push(msg);
console_log(msg);
};
newlog.read = function () { return messages.slice(); };
newlog.clear = function () { messages.length = 0; };
newlog.lines = function (delim) {
delim = typeof delim !== 'undefined' ? delim : "\n";
return messages.join(delim);
};
newlog.detach = function () { console.log = oldlog; };
return newlog;
})(console.log);
//todo: manager object allowing attach/detach without re-evaluating the closure.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment