Skip to content

Instantly share code, notes, and snippets.

@winguse
Created December 3, 2019 11:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save winguse/d6fccf2c6f6725620ea9cf35cc77dda7 to your computer and use it in GitHub Desktop.
Save winguse/d6fccf2c6f6725620ea9cf35cc77dda7 to your computer and use it in GitHub Desktop.
minium log implementation
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