Add a monkeyPatch function rather than monkey-patching in the constructor

This commit is contained in:
Kegan Dougal 2017-01-20 11:56:11 +00:00
parent 53a16158c7
commit ea860807c4
1 changed files with 5 additions and 2 deletions

View File

@ -36,7 +36,9 @@ const FLUSH_RATE_MS = 30 * 1000;
class ConsoleLogger {
constructor() {
this.logs = "";
}
monkeyPatch(consoleObj) {
// Monkey-patch console logging
const consoleFunctionsToLevels = {
log: "I",
@ -46,8 +48,8 @@ class ConsoleLogger {
};
Object.keys(consoleFunctionsToLevels).forEach((fnName) => {
const level = consoleFunctionsToLevels[fnName];
let originalFn = window.console[fnName].bind(window.console);
window.console[fnName] = (...args) => {
let originalFn = consoleObj[fnName].bind(consoleObj);
consoleObj[fnName] = (...args) => {
this.log(level, ...args);
originalFn(...args);
}
@ -310,6 +312,7 @@ module.exports = {
return initPromise;
}
logger = new ConsoleLogger();
logger.monkeyPatch(window.console);
if (window.indexedDB) {
store = new IndexedDBLogStore(window.indexedDB, logger);
initPromise = store.connect();