Created
March 7, 2019 11:03
-
-
Save rostegg/4470edefb8f0098c45e035fccb90f64e to your computer and use it in GitHub Desktop.
Refactored version of this (https://gist.github.com/rostegg/0f3b42dee9fc657fd4c36a6a8f843ef2) with few new functions
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
/* | |
Example of usage | |
let logger = new Logger('test',{ style : STYLES.RUBY_TEXT_WHITE_BACKGROUND}); | |
logger.log("test"); | |
Better setup console to dark theme and use styles with light background | |
*/ | |
'use strict'; | |
const DEFAULT_STYLE = 'background: white; color: black; display: block;'; | |
const DEFAULT_GROUP = 'DEFAULT_GROUP'; | |
const STYLES = { | |
WHITE_TEXT_BLACK_BACKGROUND_BLOCK : 'background: white; color: white; display: block;', | |
WHITE_TEXT_ALICE_BACKGROUND_BLOCK : 'background: #107896; color: white; display: block;', | |
WHITE_TEXT_RUBY_BACKGROUND_BLOCK : 'background: #C02F1D; color: white; display: block;', | |
WHITE_TEXT_INDIGO_BACKGROUND_BLOCK : 'background: #0D3D56; color: white; display: block;', | |
ALICE_TEXT_WHITE_BACKGROUND_BLOCK : 'background: white; color: #107896; display: block;', | |
RUBY_TEXT_WHITE_BACKGROUND_BLOCK : 'background: white; color: #C02F1D; display: block;', | |
INDIGO_TEXT_WHITE_BACKGROUND_BLOCK : 'background: white; color: #0D3D56; display: block;', | |
WHITE_TEXT_BLACK_BACKGROUND : 'background: white; color: white;', | |
WHITE_TEXT_ALICE_BACKGROUND : 'background: #107896; color: white;', | |
WHITE_TEXT_RUBY_BACKGROUND : 'background: #C02F1D; color: white;', | |
WHITE_TEXT_INDIGO_BACKGROUND : 'background: #0D3D56; color: white;', | |
ALICE_TEXT_WHITE_BACKGROUND : 'background: white; color: #107896;', | |
RUBY_TEXT_WHITE_BACKGROUND : 'background: white; color: #C02F1D;', | |
INDIGO_TEXT_WHITE_BACKGROUND : 'background: white; color: #0D3D56;', | |
} | |
class Logger{ | |
constructor (tag = 'DEFAULT_TAG', {style = DEFAULT_STYLE, group = DEFAULT_GROUP} = {}){ | |
this.tag = tag; | |
this.style = style; | |
this.group = group; | |
this.messageTemplate = `%c${this._time()}[${this.tag}]:`; | |
} | |
error(message){ | |
console.error(`${this.messageTemplate} ${message}`, this.style); | |
} | |
log(message){ | |
console.log(`${this.messageTemplate} ${message}`, this.style); | |
} | |
warn(message){ | |
console.warn(`${this.messageTemplate} ${message}`,this.style); | |
} | |
info(message){ | |
console.info(`${this.messageTemplate} ${message}`,this.style); | |
} | |
object(objectName,object){ | |
console.log(`${this.messageTemplate}`, this.style); | |
console.groupCollapsed(`%c\tObject \'${objectName}\'`,this.style); | |
this._printObject(object); | |
console.groupEnd(); | |
} | |
// start and stop by one functuion | |
timer(){ | |
if (this.start){ | |
this.end = window.performance.now(); | |
let time = this.end - this.start; | |
this.start = 0; | |
this.info(`Timer has stoped: ${time}ms`); | |
} | |
else{ | |
this.start = window.performance.now(); | |
} | |
} | |
table(object,columns){ | |
console.table(object, (columns)? columns:null); | |
} | |
_printObject(obj){ | |
for (let property in obj) { | |
if (!this._isObject(obj[property])) | |
console.log(`%c\t${property}: ${obj[property]}`,this.style); | |
else { | |
console.groupCollapsed(`%c\t ${property}:`,this.style); | |
this._printObject(obj[property]); | |
} | |
} | |
if (obj === undefined) | |
console.groupEnd(); | |
} | |
_isObject(val){ | |
return ( (val !== null) && (typeof val === 'function') || (typeof val === 'object') && !(Array.isArray(val))); | |
} | |
_time(){ | |
return (`[${new Date().toString()}]`); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment