Created
May 31, 2017 19:44
-
-
Save lbogdan/75be2d412d8e6c9d64a909e1d019fbe6 to your computer and use it in GitHub Desktop.
Generic 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 logLevels = { | |
| error: { | |
| prio: 0, // lower has priority | |
| method: "error" | |
| }, | |
| warning: { | |
| prio: 1, | |
| method: "warn" | |
| }, | |
| info: { | |
| prio: 2, | |
| method: "log" | |
| } | |
| }; | |
| var logLevelPrio = logLevels["info"].prio; | |
| var log = function(level, msg) { | |
| if(level in logLevels) { | |
| if(logLevels[level].prio <= logLevelPrio) { // lower has priority | |
| console[logLevels[level].method](msg); | |
| } | |
| } | |
| }; | |
| var setLogLevel = function(level) { | |
| logLevelPrio = level in logLevels ? | |
| logLevels[level].prio : -1; | |
| }; | |
| ["info", "warning", "error", "none"].forEach(function(level) { | |
| console.log("log level is", level); | |
| setLogLevel(level); | |
| log("info", "info message"); | |
| log("warning", "warning message"); | |
| log("error", "error message"); | |
| console.log(); | |
| }); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Outputs