I am currently busy writting a javascript library. In that library I want to provide some logging about what is going to the console.
function log () {
if ((window && typeof (window.console) === "undefined") || !enableLogging) {
return false;
}
function currentTime() {
var time = new Date();
return time.getHours() + ':' + time.getMinutes() + ':' + time.getSeconds() + '.' + time.getMilliseconds();
}
var args = [];
args.push(currentTime());
for (var i = 1; i < arguments.length; i++) {
args.push(arguments[i]);
}
switch (arguments[0]) {
case severity.exception:
if (window.console.exception) {
window.console.exception.apply(console, args);
} else {
window.console.error.apply(console, args);
}
break;
case severity.error:
window.console.error.apply(console, args);
break;
case severity.warning:
window.console.warning.apply(console, args);
break;
case severity.information:
window.console.log.apply(console, args);
break;
default:
window.console.log.apply(console, args);
}
return true;
}
The code above presents my log function. When called I provide at least a sevirity, a message and optionally some objects (can be DOM objects like IDBTransaction, IDBDatabase, ...).
log(severity.information, 'DB opened', db);
Now the problem I have is that this results in a memory leak. The problem is that the objects that I pass stay in memory by the console.log method. Is there a way to avoid this or clean this up?