Last active
August 19, 2016 05:31
-
-
Save stevenschobert/4377a41a319cfb2d7fb0931f4b71f499 to your computer and use it in GitHub Desktop.
My personal Log utility. Has option to set log level.
This file contains hidden or 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
var Log = { | |
_levelInt: function levelInt() { | |
return this._levels.indexOf(this.level); | |
}, | |
_intForLevel: function intForLevel(level) { | |
return this._levels.indexOf(level); | |
}, | |
_logForLevel: function logForLevel() { | |
if (!console) { return; } | |
var args = [].slice.call(arguments); | |
var level = args.shift(); | |
var message = args.shift(); | |
var method = level === "error" ? console.error : console.log; | |
if (this._levelInt() > this._intForLevel(level)) { return; } | |
method.apply(console, ["["+ level.toUpperCase() +"] " + message].concat(args)); | |
}, | |
debug: function debug() { | |
var args = [].slice.call(arguments); | |
args.unshift("debug"); | |
this._logForLevel.apply(this, args); | |
}, | |
info: function debug() { | |
var args = [].slice.call(arguments); | |
args.unshift("info"); | |
this._logForLevel.apply(this, args); | |
}, | |
error: function debug() { | |
var args = [].slice.call(arguments); | |
args.unshift("error"); | |
this._logForLevel.apply(this, args); | |
} | |
}; | |
Log._levels = ["debug", "info", "error"]; | |
Log.level = "debug"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment