Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
logger example
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