Skip to content

Instantly share code, notes, and snippets.

@Eccenux
Last active October 11, 2016 18:02
Show Gist options
  • Save Eccenux/d891b3387f9179e36664790a123b11a6 to your computer and use it in GitHub Desktop.
Save Eccenux/d891b3387f9179e36664790a123b11a6 to your computer and use it in GitHub Desktop.
Utility for console logs preservation.
/**
* Utility for console logs preservation.
*
* @example Enable logs preservation for next page load:
* logPreservation.enable();
*
* @example Dump, but keep log entries for another dump:
* logPreservation.dump(true);
*
* @author Maciej Nux Jaros
* @license MIT
*/
function LogPreservation() {
this.saved = [];
this.failInitError = false;
this.origConsole = {};
this.restoreSettings();
if (this.settings.enabled) {
this.init();
} else {
this.failInitError = 'disabled';
}
}
LogPreservation.prototype.init = function() {
try {
var me = this;
this.capturedFunctions.forEach(function(level){
me.origConsole[level] = console[level];
console[level] = function() {
me._save(level, arguments);
me.origConsole[level].apply(console, arguments);
};
});
} catch (e) {
// save for debugging
this.failInitError = e;
}
};
// functions to replace and capture
LogPreservation.prototype.capturedFunctions = ['log', 'warn', 'error'];
// settings save key
LogPreservation.prototype.settingsKey = 'LogPreservation-settings-storage';
// settings
LogPreservation.prototype.settings = {
enabled : false
};
/**
* Enable for next time or temporarily pause saving logs.
*/
LogPreservation.prototype.enable = function() {
this.settings.enabled = true;
this.saveSettings();
};
/**
* Disable for next time.
*/
LogPreservation.prototype.disable = function() {
this.settings.enabled = false;
this.saveSettings();
};
/**
* Store settings ~permanently.
*/
LogPreservation.prototype.saveSettings = function() {
localStorage.setItem(this.settingsKey, JSON.stringify(this.settings));
};
/**
* Clear settings storage.
*/
LogPreservation.prototype.clearSettings = function() {
localStorage.removeItem(this.settingsKey);
};
/**
* Restore settings.
*
* @note missing keys will not be removed from current settings.
*/
LogPreservation.prototype.restoreSettings = function() {
var settings = JSON.parse(localStorage.getItem(this.settingsKey));
if (settings) {
for (var i in settings) {
this.settings[i] = settings[i];
}
}
};
/**
* Save console data.
*
* @private
*
* @param {String} level
* @param {Array} argumentsArray
* This is either arguments array or a real Array object
* (see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope/arguments).
* @returns {String}
*/
LogPreservation.prototype._save = function (level, argumentsArray) {
if (!this.settings.enabled) {
return;
}
this.saved.push({
level : level,
argumentsArray : argumentsArray,
dt : (new Date()).toISOString()
});
};
/**
* Dump all saved messages to console.
* @param {Boolean} preserve if true then messages are not cleared after dump.
* @returns {undefined}
*/
LogPreservation.prototype.dump = function (preserve) {
// disable pre-dump
var enabledPrev = this.settings.enabled;
this.settings.enabled = false;
for (var i = 0; i < this.saved.length; i++) {
var level = this.saved[i].level;
var argumentsArray = this.saved[i].argumentsArray;
// doesn't work on iOS (at least from weinre)
//this.origConsole[level].apply(console, argumentsArray);
console[level].apply(console, argumentsArray);
}
// reset state post-dump
this.settings.enabled = enabledPrev;
if (!preserve) {
this.saved.length = 0;
}
};
var logPreservation = new LogPreservation();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment