Skip to content

Instantly share code, notes, and snippets.

@aronanda
Last active July 29, 2018 19:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aronanda/2816914c41488bc9b70c714d4b225951 to your computer and use it in GitHub Desktop.
Save aronanda/2816914c41488bc9b70c714d4b225951 to your computer and use it in GitHub Desktop.
JS Logger
const DEBUG = true
// const DEBUG = process.env.NODE_ENV !== 'production'
const LOG_DATE = true
const LOG_LOCALE = "en-US"
const LOG_DATE_OPTS = { year: 'numeric', month: 'long', day: 'numeric', hour: '2-digit', minute: '2-digit', second: '2-digit' }
const log = function (level, ...args) {
if (DEBUG) {
let prefix = ''
if (typeof level === 'string' && [ 'log', 'warn', 'error' ].includes(level)) {
prefix = level.toUpperCase()
if (LOG_DATE) {
prefix += ' | ' + (new Date()).toLocaleString(LOG_LOCALE, LOG_DATE_OPTS)
}
prefix = '[ ' + prefix + ' ] '
} else {
args.unshift(level)
level = 'log'
}
if (prefix !== '')
args.unshift(prefix)
console[level].apply(console, args)
}
}
const log = (level, ...args) => {
if (typeof level !== 'string' || !([ 'log', 'warn', 'error' ].includes(level)))
level = args.unshift(level) && 'log'
console[level].bind(console)(...args)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment