Last active
February 26, 2018 09:20
-
-
Save brandonb927/4955417 to your computer and use it in GitHub Desktop.
JS: logging function with color coded output
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
// Ablity to turn off console in production environment | |
var logify = function() { | |
var oldConsoleLog = null, | |
isObject = false, | |
pub = {} | |
if(typeof message === 'object') { | |
isObject = true | |
} | |
pub.log = function log(message) { | |
if(!isObject) { | |
console.log('%c[Log] ' + message, 'color: black') | |
} else { | |
console.log(message) | |
} | |
} | |
pub.info = function info(message) { | |
if(!isObject) { | |
console.log('%c[Info] ' + message, 'color: blue') | |
} else { | |
console.log(message) | |
} | |
} | |
pub.warn = function warn(message) { | |
if(!isObject) { | |
console.log('%c[Warning] ' + message, 'color: orange') | |
} else { | |
console.log(message) | |
} | |
} | |
pub.error = function error(message) { | |
if(!isObject) { | |
console.log('%c[Error] ' + message, 'color: red') | |
} else { | |
console.log(message) | |
} | |
} | |
pub.enableLogger = function enableLogger() { | |
if(oldConsoleLog == null) { | |
return | |
} | |
window['console']['log'] = oldConsoleLog | |
} | |
pub.disableLogger = function disableLogger() { | |
oldConsoleLog = console.log | |
window['console']['log'] = function() {} | |
window['console']['info'] = function() {} | |
window['console']['warn'] = function() {} | |
window['console']['error'] = function() {} | |
} | |
return pub | |
}() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment