Created
December 3, 2019 11:15
-
-
Save winguse/d6fccf2c6f6725620ea9cf35cc77dda7 to your computer and use it in GitHub Desktop.
minium log implementation
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
const LOG_LEVELS = ['DEBUG', 'INFO', 'WARN', 'ERROR'] | |
const [LOG_DEBUG, LOG_INFO, LOG_WARN, LOG_ERROR] = LOG_LEVELS; | |
const LOG_LEVEL = LOG_DEBUG; | |
function makeLogFunc(level, func = console.log) { | |
return LOG_LEVELS.indexOf(level) >= LOG_LEVELS.indexOf(LOG_LEVEL) ? (...msg) => { | |
const now = new Date().toLocaleString(); | |
func(...[now, level, ...msg]); | |
} : () => {}; | |
} | |
const log = { | |
debug: makeLogFunc(LOG_DEBUG), | |
info: makeLogFunc(LOG_INFO), | |
warn: makeLogFunc(LOG_WARN), | |
error: makeLogFunc(LOG_ERROR, console.error), | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment