Created
June 16, 2019 16:27
-
-
Save b6pzeusbc54tvhw5jgpyw8pwz2x6gs/c58e47dc8ea22e375638fc59227bd37f to your computer and use it in GitHub Desktop.
logger example
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 prioritized = { | |
error: 0, | |
warn: 1, | |
info: 2, | |
verbose: 3, | |
debug: 4, | |
silly: 5 | |
} | |
let level = 'debug' | |
export const setLevel = (l) => { | |
if (prioritized[l] === void 0) { | |
console.warn('warn [file:logger.js]') | |
return | |
} | |
level = l | |
} | |
export const createLogger = (type='-') => { | |
const logger = { | |
debug: (...msg) => { | |
if (prioritized[level] < prioritized.debug) return | |
console.debug(`debug [${type}] ${msg}`) | |
}, | |
info: (...msg) => { | |
if (prioritized[level] < prioritized.info) return | |
console.info(`info [${type}] ${msg}`) | |
}, | |
warn: (msg) => { | |
if (prioritized[level] < prioritized.warn) return | |
console.warn(`warn [${type}] ${msg}`) | |
}, | |
error: (msg) => { | |
if (prioritized[level] < prioritized.error) return | |
console.error(`error [${type}] ${msg}`) | |
}, | |
} | |
return logger | |
} | |
export const logError = (msg) => { | |
const { errorNo, message, description } = msg | |
if (!errorNo || !message) return console.error('error [file:logger.js] can not parse errorLog: ' + msg) | |
console.error(`error [ERROR] ${errorNo} "${message}" "${description || '-'}"` + message) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment