Last active
November 2, 2019 10:49
-
-
Save lski/feec56451600e38ad29d8f9b36064d74 to your computer and use it in GitHub Desktop.
Create a logger that will only log values if the initial currentLevel is equal to or greater than the initially passed in level
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
/** | |
* Creates a logger that optional runs depending on the current level set for logging in the application | |
* | |
* Levels: | |
* 0 = debug | |
* 1 = trace (alias for verbose) | |
* 2 = info | |
* 3 = warn | |
* 4 = error | |
* 5+ = none | |
* | |
* @param {number} currentLevel The current log level for the application. | |
*/ | |
const createLogger = (currentLevel) => { | |
function logCreator(currentLevel, criteriaLevel, logFunctionName) { | |
return (...args) => { | |
// If the currentLevel is lower than the criteria level, then simply return the first argument | |
if (currentLevel < criteriaLevel) { | |
return args[0]; | |
} | |
// We should look at logging out an object, with timestamps etc, but this will surffice to start | |
console[logFunctionName](`Log ${logFunctionName}:`, ...args); | |
return args[0]; | |
}; | |
} | |
const debug = logCreator(currentLevel, 0, 'debug'); | |
const trace = logCreator(currentLevel, 1, 'trace'); | |
const info = logCreator(currentLevel, 2, 'info'); | |
const warn = logCreator(currentLevel, 3, 'warn'); | |
const error = logCreator(currentLevel, 4, 'error'); | |
return { | |
debug, | |
trace, | |
verbose: trace, | |
info, | |
warn, | |
error | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment