Skip to content

Instantly share code, notes, and snippets.

@yan-foto
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yan-foto/ee25fbf0edc2401c292d to your computer and use it in GitHub Desktop.
Save yan-foto/ee25fbf0edc2401c292d to your computer and use it in GitHub Desktop.
Simple JavaScript logger with log level and (graceful) fallback
Logger = function(level) {
this.level = level || 0;
methods = ["trace", "debug", "info", "warn", "error"];
levels = {};
this.log = function() {
(console.log === undefined) ? function() {} : console.log.apply(console, arguments);
};
methods.forEach(function(level, index) {
levels[level] = index;
this[level] = function() {
if(this.level - 1 > levels[level]) {
return; // No need to log at all!
}
(console[level] === undefined) ? this.log.apply(null, arguments) : console[level].apply(console, arguments);
};
}.bind(this));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment