Created
May 5, 2017 12:14
-
-
Save embiem/fd8684a1cb6b101cdca7a1a4b45266d7 to your computer and use it in GitHub Desktop.
Custom logging via ES6 class. Extendable.
This file contains hidden or 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
class Logger { | |
constructor() { | |
if (!Logger.instance) { | |
Logger.instance = this; | |
} | |
return Logger.instance; | |
} | |
log(...args) { | |
console.log(...args); | |
} | |
warn(...args) { | |
console.warn(...args); | |
} | |
error(...args) { | |
console.error(...args); | |
} | |
} | |
// ensure Singleton | |
const instance = new Logger(); | |
Object.freeze(instance); | |
export default instance; |
You could minimize all this robust code with this implementation:
const logger = { log: (...args) => console.log(...args), warn: (...args) => console.warn(...args), error: (...args) => console.error(...args), debug: (...args) => console.debug(...args), info: (...args) => console.info(...args), table: (...args) => console.table(...args), }; export default logger;
@sayeko nice, does yours export a singleton?
yes this is a singleton.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You could minimize all this robust code with this implementation: