Created
August 20, 2015 16:14
-
-
Save leesei/dd3a7a0a49fa29294e18 to your computer and use it in GitHub Desktop.
Logger to Browser console (with colors)
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
// http://charlesbking.com/power_of_es6/#/21 | |
//create a logger facade | |
class Logger { | |
//constructor | |
constructor (type = "Info") { | |
this.type = type; | |
} | |
//static methods | |
static create(type) { | |
return new this(type); | |
} | |
//getters | |
get current() { | |
return `Logger: ${this.type}`; | |
} | |
//and setters | |
set current(type) { | |
this.type = type; | |
} | |
log (message) { | |
let msg = `%c ${new Date().toISOString()}: ${message}`; | |
switch (this.type) { | |
case "Info": | |
console.log(msg, | |
'background:#659cef;color:#fff;font-size:14px;' | |
); | |
break; | |
case "Error": | |
console.log(msg, | |
'background: red; color: #fff;font-size:14px;' | |
); | |
break; | |
case "Debug": | |
console.log(msg, | |
'background: #e67e22; color:#fff; font-size:14px;' | |
); | |
break; | |
default: | |
console.log(msg); | |
} | |
} | |
} | |
//create an instance of our logger | |
const debugLogger = new Logger("Debug"); | |
debugLogger.log("Hello"); | |
debugLogger.log(debugLogger.current); | |
//extend it | |
class ConfigurableLogger extends Logger { | |
//getters | |
get current() { | |
return `ConfigurableLogger: ${this.type}`; | |
} | |
log (message, type) { | |
this.type = type; | |
super.log(message); | |
} | |
} | |
//create instance of our configurable logger | |
const cLogger = ConfigurableLogger.create("Debug"); | |
cLogger.log("Configurable Logger", "Info"); | |
cLogger.log(cLogger.current); | |
cLogger.log(cLogger instanceof ConfigurableLogger); // true | |
cLogger.log(cLogger instanceof Logger); // true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment