/** Valid logLevel values */ export type LOG_LEVEL = - 1 | 0 | 1 | 2 | 3 | 4 | 5 | 'none' | 'error' | 'warn' | 'info' | 'verbose' | 'debug' | 'silly' ; /** Log level string to number map */ const LOG_LEVELS = { none : - 1 , error : 0 , warn : 1 , info : 2 , verbose : 3 , debug : 4 , silly : 5 } ; /** All CSS requried to print labels */ const STYLES = { error : 'color: white; background: #db2828; border-radius: 8px' , warn : 'color: white; background: #fbbd08; border-radius: 8px' , info : 'color: white; background: #c4c64f; border-radius: 8px' , verbose : 'color: white; background: #6435c9; border-radius: 8px' , debug : 'color: white; background: #2185d0; border-radius: 8px' , silly : 'color: white; background: #21ba45; border-radius: 8px' , tag : 'color: black; background: #5bfff4; border-radius: 8px' } ; enum LOG_ITEM_TYPE { text = 'text' , image = 'image' } /**
 * Log item interface
 * This is the data structure for hidden logs
 */ interface ILogItemText { /** Type of message */ type : LOG_ITEM_TYPE . text ; /** Log level of message */ level : number ; /** Log tag for the message */ tag : string ; /** Log message */ message : string ; /** Additional arguments passed on to `console` methods */ parts : any [ ] ; /** URL of image if type is image */ url ? : string ; } interface ILogItemImage { type : LOG_ITEM_TYPE . image ; level : number ; url : string ; callback ? : { ( image ) : void ; } ; } type ILogItem = ILogItemText | ILogItemImage ; /**
 * Console method interface
 */ interface IConsoleMethod { param message param optionalParams ( message ? , ... optionalParams ) : void ; } author export default class LogT { /** Log level, above which logs will be printed to console */ private logLevel : number = LOG_LEVELS . none ; /** Logs which are hidden - not been printed to console */ private hidden : ILogItem [ ] = [ ] ; private loggerMap : { [ key ] : { text : string ; logger : IConsoleMethod ; } ; } = { 0 : { text : 'error' , logger : console . error } , 1 : { text : 'warn' , logger : console . warn } , 2 : { text : 'info' , logger : console . info } , 3 : { text : 'verbose' , logger : console . log } , 4 : { text : 'debug' , logger : console . debug } , 5 : { text : 'silly' , logger : console . log } } ; param logLevel constructor ( logLevel ) { this . setLogLevel ( logLevel ) ; } param level param tag param message param parts private log = ( level , tag , message , ... parts ) => { if ( level <= this . logLevel ) { if ( this . loggerMap [ level ] ) { const loggerToUse = this . loggerMap [ level ] . logger ; loggerToUse ( `%c ${ this . loggerMap [ level ] . text } %c %c ${ tag } ` , STYLES [ this . loggerMap [ level ] . text ] , '' , STYLES . tag , message , ... parts ) ; } } else { this . hidden . push ( { type : LOG_ITEM_TYPE . text , level , tag , message , parts } ) ; } } ; /**
     * Get instance log level
     */ public getLogLevel = ( ) : number => this . logLevel ; private getFinalLevel = ( level ) : number => { if ( level != null ) { if ( typeof level === 'string' ) { if ( LOG_LEVELS [ level ] != null ) { return LOG_LEVELS [ level ] ; } } if ( typeof level === 'number' ) { if ( level >= LOG_LEVELS . none && level <= LOG_LEVELS . silly ) { return level ; } } } return LOG_LEVELS . none ; } ; param logLevel public setLogLevel = ( level ) => { this . logLevel = this . getFinalLevel ( level ) ; } ; param tag param message param parts public error = ( tag , message , ... parts ) => { this . log ( LOG_LEVELS . error , tag , message , ... parts ) ; } ; param tag param message param parts public warn = ( tag , message , ... parts ) => { this . log ( LOG_LEVELS . warn , tag , message , ... parts ) ; } ; param tag param message param parts public info = ( tag , message , ... parts ) => { this . log ( LOG_LEVELS . info , tag , message , ... parts ) ; } ; param tag param message param parts public verbose = ( tag , message , ... parts ) => { this . log ( LOG_LEVELS . verbose , tag , message , ... parts ) ; } ; param tag param message param parts public debug = ( tag , message , ... parts ) => { this . log ( LOG_LEVELS . debug , tag , message , ... parts ) ; } ; param tag param message param parts public silly = ( tag , message , ... parts ) => { this . log ( LOG_LEVELS . silly , tag , message , ... parts ) ; } ; param logLevel public showHidden = ( logLevel ) => { const oldLogLevel = this . logLevel ; this . setLogLevel ( logLevel ) ; const currentHidden = this . hidden ; this . hidden = [ ] ; currentHidden . forEach ( logItem => { if ( logItem . type === LOG_ITEM_TYPE . text ) { this . log ( logItem . level , logItem . tag , logItem . message , ... logItem . parts ) ; } else if ( logItem . type === LOG_ITEM_TYPE . image ) { this . logImage ( logItem . level , logItem . url , logItem . callback ) ; } } ) ; this . setLogLevel ( ( oldLogLevel as LOG_LEVEL ) ) ; } ; /**
     * Method to override `window.console` methods
     */ public readConsole = ( ) => { const TAG = 'console' ; console . error = ( message ? , ... parts ) => { this . error ( TAG , message , ... parts ) ; } ; console . warn = ( message ? , ... parts ) => { this . warn ( TAG , message , ... parts ) ; } ; console . info = ( message ? , ... parts ) => { this . info ( TAG , message , ... parts ) ; } ; console . log = ( message ? , ... parts ) => { this . debug ( TAG , message , ... parts ) ; } ; } ; private logImage = ( level , url , callback ? ) => { const image = new Image ( ) ; let loaded = false ; image . onload = ( ) => { if ( ! loaded ) { loaded = true ; const style = [ 'font-size: 1px;' , `line-height: ${ image . height % 2 }px;` , `padding: ${ image . height * 0.5 }px ${ image . width * 0.5 }px;` , `background-size: ${ image . width }px ${ image . height }px;` , `background: url('${ url }') no-repeat;` ] . join ( ' ' ) ; if ( level <= this . logLevel ) { let loggerToUse = this . loggerMap [ LOG_LEVELS . verbose ] . logger ; if ( this . loggerMap [ level ] ) { loggerToUse = this . loggerMap [ level ] . logger ; } loggerToUse ( '%c ' , style ) ; } else { this . hidden . push ( { type : LOG_ITEM_TYPE . image , level , url , callback } ) ; } } } ; image . src = url ; if ( callback ) { callback ( image ) ; } } ; param level param url param callback public image = ( level , url , callback ? ) => { this . logImage ( this . getFinalLevel ( level ) , url , callback ) ; } ; }