Created
August 6, 2017 13:01
-
-
Save agoldis/1eee1c4571fd441b2ffb1649466f4cea to your computer and use it in GitHub Desktop.
Basic logger for node
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 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