Last active
July 29, 2018 19:13
-
-
Save aronanda/2816914c41488bc9b70c714d4b225951 to your computer and use it in GitHub Desktop.
JS Logger
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 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) | |
} | |
} |
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 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