Skip to content

Instantly share code, notes, and snippets.

@winguse
Created December 3, 2019 11:15
Embed
What would you like to do?
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