Created
December 5, 2014 23:24
-
-
Save jednano/c3a57792f6de83fdfba6 to your computer and use it in GitHub Desktop.
Isomorphic Logger
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
/*eslint-disable no-console */ | |
var LOG_METHODS = ['debug', 'error', 'info', 'warn']; | |
function log() { | |
if (!console || !console.log) { | |
return; | |
} | |
console.log.apply(console, arguments); | |
} | |
function proxyLog(logMethod, label) { | |
return function() { | |
var args = Array.prototype.slice.call(arguments); | |
label = label && label.toUpperCase(); | |
if (console && console[logMethod]) { | |
if (label) { | |
args.unshift('[' + label + ']'); | |
} | |
console[logMethod].apply(console, args); | |
return; | |
} | |
args.unshift('[' + label || logMethod.toUpperCase() + ']'); | |
log.apply(log, args); | |
}; | |
} | |
for (var i = 0; i < LOG_METHODS.length; i++) { | |
var logMethod = LOG_METHODS[i]; | |
log[logMethod] = proxyLog(logMethod); | |
} | |
log.deprecate = proxyLog('warn', 'deprecation'); | |
module.exports = log; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment