Skip to content

Instantly share code, notes, and snippets.

@agoldis
Created August 6, 2017 13:01
Show Gist options
  • Save agoldis/1eee1c4571fd441b2ffb1649466f4cea to your computer and use it in GitHub Desktop.
Save agoldis/1eee1c4571fd441b2ffb1649466f4cea to your computer and use it in GitHub Desktop.
Basic logger for node
const LOGLEVELS = {
mute: -1,
error: 0,
warn: 1,
info: 2,
debug: 3
}
let currentLevel = LOGLEVELS.mute
function setLevel (level) {
if (typeof LOGLEVELS[level] === 'undefined') {
currentLevel = LOGLEVELS['mute'];
return;
}
currentLevel = LOGLEVELS[level];
}
function log (level, msg) {
if (currentLevel >= level) {
let fn = typeof console[level] === 'function' || console.log
fn(`${level}: ${msg}`)
}
}
export default {
setLevel,
error: log.bind(null, 'error'),
warn: log.bind(null, 'warn'),
info: log.bind(null, 'info'),
debug: log.bind(null, 'debug')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment