Last active
April 21, 2017 06:23
-
-
Save dfdgsdfg/4b525fe5e0ce5a861bf668319da22eaa to your computer and use it in GitHub Desktop.
Simple logger for browser side
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
/* eslint-disable no-console, consistent-return */ | |
const logger = (function Logger() { | |
const prefix = "[MYAPP]"; | |
const logLevelPreset = [ | |
'SILENT', | |
'ERROR', | |
'WARN', | |
'INFO', | |
'DEBUG', | |
'VERBOSE', | |
]; | |
let level; | |
function init(config) { | |
if (!logLevelPreset.includes(config)) { | |
throw new Error('Logger:UnmatchedLogLevel'); | |
} | |
level = config; | |
} | |
function e(...message) { | |
if (level === 'SILENT') { | |
return; | |
} | |
return console.error(prefix+"E>", ...message); | |
} | |
function w(...message) { | |
if (level === 'SILENT' || level === 'ERROR') { | |
return; | |
} | |
return console.warn(prefix+"W>", ...message); | |
} | |
function g(...message) { | |
if (level === 'SILENT' || level === 'ERROR' || level === 'WARN') { | |
return; | |
} | |
return console.group(prefix+"G>", ...message); | |
} | |
function gEnd() { | |
if (level === 'SILENT' || level === 'ERROR' || level === 'WARN') { | |
return; | |
} | |
return console.groupEnd(); | |
} | |
function l(...message) { | |
if (level === 'SILENT' || level === 'ERROR' || level === 'WARN') { | |
return; | |
} | |
return console.info(prefix+"I>", ...message); | |
} | |
function i(...message) { | |
if (level === 'SILENT' || level === 'ERROR' || level === 'WARN') { | |
return; | |
} | |
return console.info(prefix+"I>", ...message); | |
} | |
function d(...message) { | |
if (level === 'SILENT' || level === 'ERROR' || level === 'WARN' || level === 'INFO') { | |
return; | |
} | |
return console.debug(prefix+"I>", ...message); | |
} | |
function v(...message) { | |
if (level === 'SILENT' || level === 'ERROR' || level === 'WARN' || level === 'INFO' || level === 'DEBUG') { | |
return; | |
} | |
return console.debug(prefix+"D>", ...message); | |
} | |
return Object.freeze({ | |
init, | |
e, | |
w, | |
g, | |
gEnd, | |
l, | |
i, | |
d, | |
v, | |
}); | |
}()); | |
export default logger; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment