I'd like to use a modified version of this solution, to wrap console logging into a new function:
function Logger(force) {
const logging = {};
if (force) {
for (let m in console) {
if (typeof console[m] == 'function') {
logging[m] = console[m].bind(console);
}
}
} else {
for (let m in console) {
if (typeof console[m] == 'function') {
logging[m] = function() {};
}
}
}
return logging;
}
debugOff = new Logger(false);
debugOff.log("this WILL NOT log");
debugOn = new Logger(true);
debugOn.log("This WILL log");
However, I'd like to not create a new instance of the Logger
, and just pass true/false
with the relevant string
. I've tried wrapping the new instance in another function, but I'm having to use a specific method in it:
function noopLogger (force, string) {
function Logger(force) {
const logging = {};
if (force) {
for (let m in console) {
if (typeof console[m] == 'function') {
logging[m] = console[m].bind(console);
}
}
} else {
for (let m in console) {
if (typeof console[m] == 'function') {
logging[m] = function() {};
}
}
}
return logging;
}
instance = new Logger(force);
return instance.log(string); // don't want to do this here
}
noopLogger(true, "this will log"); // this logs, but I only have access to log, not warn, error, etc.
How can I refactor this so I can call:
noopLogger.log(true, 'this WILL log');
noopLogger.log(false, 'this WILL NOT log');