Last active
May 29, 2018 15:57
-
-
Save TheRedDev/75eea831fdd845e7ff106d224c663afd to your computer and use it in GitHub Desktop.
JS Bin// source https://jsbin.com/voqihuw
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Logger = function(enable, prefix) { | |
this.log = {}; | |
if ((typeof globalEnableLogger !== 'undefined' && globalEnableLogger == false) || enable == false) { | |
for(var m in console) { | |
if(typeof console[m] == 'function') { | |
this.log[m] = function(){}; | |
} | |
} | |
} else if (enable && typeof enable === 'string') { | |
for (var m in console) { | |
if (typeof console[m] == 'function') { | |
this.log[m] = console[m].bind(window.console, enable+":"); | |
} | |
} | |
} else if (enable && prefix) { | |
for (var m in console) { | |
if (typeof console[m] == 'function') { | |
this.log[m] = console[m].bind(window.console, prefix+":"); | |
} | |
} | |
} else if (enable || typeof enable === 'undefined') { | |
for (var m in console) { | |
if (typeof console[m] == 'function') { | |
this.log[m] = console[m].bind(window.console); | |
} | |
} | |
} | |
return this.log; | |
} | |
//var globalEnableLogger = false; | |
var logPlain = Logger(); | |
var logUndef = Logger(undefined); | |
var logTrue = Logger(true); | |
var logFalse = Logger(false); | |
var logName = Logger(false, "MyName"); | |
var logOnlyName = Logger("NameOnly"); | |
//log some stuff | |
logPlain.info('should work'); | |
console.log('console log on the next line!'); | |
logUndef.info('should also work'); | |
logTrue.info('should definitely work'); | |
logFalse.info('should not work'); | |
logName.info('has prefix MyName'); | |
logOnlyName.info('has prefix NameOnly'); | |
var MyClass = function() { | |
this.log = Logger('MyClass'); | |
this.log.warn('It works in classses'); | |
this.log.trace('hello trace'); | |
initialize(); | |
return { | |
teststuff: teststuff, | |
funstuff: funstuff, | |
log: this.log | |
}; | |
function initialize(){ | |
this.log.trace('begin init'); | |
logName.error('test'); | |
} | |
function teststuff(msg){ | |
this.log.info('rawr4'+msg); | |
return msg+'rawr2'; | |
} | |
function funstuff(msg2){ | |
var obj = {}; | |
obj.rawr5 = 'testing3'+msg2; | |
this.log.info(obj); | |
return obj; | |
} | |
} | |
var mc = new MyClass(); | |
mc.funstuff(mc.teststuff('testing1')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment