Skip to content

Instantly share code, notes, and snippets.

@jednano
Created December 5, 2014 23:24
Show Gist options
  • Save jednano/c3a57792f6de83fdfba6 to your computer and use it in GitHub Desktop.
Save jednano/c3a57792f6de83fdfba6 to your computer and use it in GitHub Desktop.
Isomorphic Logger
/*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