Last active
August 29, 2015 14:25
-
-
Save yan-foto/ee25fbf0edc2401c292d to your computer and use it in GitHub Desktop.
Simple JavaScript logger with log level and (graceful) fallback
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
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