Skip to content

Instantly share code, notes, and snippets.

@wilcorrea
Forked from luiguild/logger.js
Created July 10, 2018 18:08
Show Gist options
  • Save wilcorrea/736e48b48b95e98fc9a5b8c591e051e0 to your computer and use it in GitHub Desktop.
Save wilcorrea/736e48b48b95e98fc9a5b8c591e051e0 to your computer and use it in GitHub Desktop.
Simple logger module and VuePlugin that check NODE_ENV to show console.log()
const prefix = process.env.PREFIX
const nodeEnv = process.env.NODE_ENV
let logCounter = 0
let errorCounter = 0
let fatalCounter = 0
let warnCounter = 0
export const vLogger = {
install (Vue, options) {
Vue.prototype.$log = log
Vue.prototype.$warn = warn
Vue.prototype.$error = error
Vue.prototype.$fatal = fatal
}
}
/**
* Logger constructor
* @param {String} type - Type of the log message
* @param {String} message - Your log text
* @param {Object} _object - If you pass an object or array, it's will be put together
* @return {Function}
*/
const logControll = (type, message, _object) => {
const object = _object !== undefined
? _object
: ''
const messageMaker = (counter, message) =>
`${logCounter} [${type}|${prefix}] ${JSON.stringify(message)}`
if (nodeEnv !== 'production') {
switch (type) {
case 'LOG':
logCounter++
return console.log(
messageMaker(logCounter, message),
object
)
case 'WARN':
warnCounter++
return console.warn(
messageMaker(warnCounter, message),
object
)
case 'ERROR':
errorCounter++
return console.error(
messageMaker(errorCounter, message),
object
)
case 'FATAL':
fatalCounter++
throw new Error(
messageMaker(fatalCounter, message)
)
default:
return console.log(message, object)
}
}
}
/**
* Add conter and prefix and execute simple console.log()
* @param {String} message - Your log text
* @param {Object} object - If you pass an object or array, it's will be put together
* @return {Function}
*/
export const log = (message, object) =>
logControll('LOG', message, object)
/**
* Add conter and prefix and execute console.warn()
* @param {String} message - Your log text
* @param {Object} object - If you pass an object or array, it's will be put together
* @return {Function}
*/
export const warn = (message, object) =>
logControll('WARN', message, object)
/**
* Add conter and prefix and execute console.error()
* @param {String} message - Your log text
* @param {Object} object - If you pass an object or array, it's will be put together
* @return {Function}
*/
export const error = (message, object) =>
logControll('ERROR', message, object)
/**
* Add conter and prefix and create a throw new Error()
* @param {String} message - Your log text
* @return {Function}
*/
export const fatal = message =>
logControll('FATAL', message)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment