import { LogLevelType } from "../enums/logLevelType" ; import { LogService as LogServiceAbstraction } from "../abstractions/log.service" ; import * as hrtime from "browser-hrtime" ; export class ConsoleLogService implements LogServiceAbstraction { protected timersMap : Map < string , [ number , number ] > = new Map ( ) ; constructor ( protected isDev , protected filter = null ) { } debug ( message ) { if ( ! this . isDev ) { return ; } this . write ( LogLevelType . Debug , message ) ; } info ( message ) { this . write ( LogLevelType . Info , message ) ; } warning ( message ) { this . write ( LogLevelType . Warning , message ) ; } error ( message ) { this . write ( LogLevelType . Error , message ) ; } write ( level , message ) { if ( this . filter != null && this . filter ( level ) ) { return ; } switch ( level ) { case LogLevelType . Debug : console . log ( message ) ; break ; case LogLevelType . Info : console . log ( message ) ; break ; case LogLevelType . Warning : console . warn ( message ) ; break ; case LogLevelType . Error : console . error ( message ) ; break ; default : break ; } } time ( label = "default" ) { if ( ! this . timersMap . has ( label ) ) { this . timersMap . set ( label , hrtime ( ) ) ; } } timeEnd ( label = "default" ) { const elapsed = hrtime ( this . timersMap . get ( label ) ) ; this . timersMap . delete ( label ) ; this . write ( LogLevelType . Info , `${ label }: ${ elapsed [ 0 ] * 1000 + elapsed [ 1 ] / 10e6 }ms` ) ; return elapsed ; } }