import { AiService } from 'app/shared/services/ai.service' ; import { Injectable } from '@angular/core' ; import { Url } from 'app/shared/Utilities/url' ; export enum LogLevel { error , warning , debug , verbose , trace } export type LogLevelString = 'error' | 'warning' | 'debug' | 'verbose' ; @ Injectable ( ) export class LogService { private _logLevel : LogLevel ; private _categories : string [ ] ; constructor ( private _aiService ) { const levelStr = Url . getParameterByName ( null , 'appsvc.log.level' ) ; if ( levelStr ) { try { this . _logLevel = LogLevel [ levelStr ] ; } catch ( e ) { this . _logLevel = LogLevel . warning ; } } else { this . _logLevel = LogLevel . warning ; } this . _categories = Url . getParameterArrayByName ( null , 'appsvc.log.category' ) ; } public trace ( category , id , data ? ) { if ( ! category || ! id ) { throw Error ( 'You must provide a category and id' ) ; } this . _aiService . trackEvent ( `/trace/${ category }/${ id }` , data ) ; if ( this . _shouldLog ( category , LogLevel . trace ) ) { console . error ( `[${ category }] - ${ this . _getDataString ( data ) }` ) ; } } public error ( category , id , data ) { if ( ! category || ! id || ! data ) { throw Error ( 'You must provide a category, id, and data' ) ; } const errorId = `/errors/${ category }/${ id }` ; this . _aiService . trackEvent ( errorId , data ) ; if ( this . _shouldLog ( category , LogLevel . error ) ) { console . error ( `[${ category }] - ${ this . _getDataString ( data ) }` ) ; } } public warn ( category , id , data ) { if ( ! category || ! id || ! data ) { throw Error ( 'You must provide a category, id, and data' ) ; } const warningId = `/warnings/${ category }/${ id }` ; this . _aiService . trackEvent ( warningId , data ) ; if ( this . _shouldLog ( category , LogLevel . warning ) ) { console . log ( `%c[${ category }] - ${ this . _getDataString ( data ) }` , 'color: #ff8c00' ) ; } } public debug ( category , data ) { if ( ! category || ! data ) { throw Error ( 'You must provide a category and data' ) ; } if ( this . _shouldLog ( category , LogLevel . debug ) ) { console . log ( `${ this . _getTime ( ) } %c[${ category }] - ${ this . _getDataString ( data ) }` , 'color: #0058ad' ) ; } } public verbose ( category , data ) { if ( ! category || ! data ) { throw Error ( 'You must provide a category and data' ) ; } if ( this . _shouldLog ( category , LogLevel . verbose ) ) { console . log ( `${ this . _getTime ( ) } [${ category }] - ${ this . _getDataString ( data ) }` ) ; } } public log ( level , category , data , id ? ) { if ( ! id && ( level === LogLevel . error || level === LogLevel . warning ) ) { throw Error ( 'Error and Warning log levels require an id' ) ; } if ( level === LogLevel . error ) { this . error ( category , id , data ) ; } else if ( level === LogLevel . warning ) { this . warn ( category , id , data ) ; } else if ( level === LogLevel . debug ) { this . debug ( category , data ) ; } else { this . verbose ( category , data ) ; } } private _shouldLog ( category , logLevel ) { if ( logLevel <= this . _logLevel ) { if ( logLevel === LogLevel . error || logLevel === LogLevel . warning ) { return true ; } else if ( this . _categories . length > 0 && this . _categories . find ( c => c . toLowerCase ( ) === category . toLowerCase ( ) ) ) { return true ; } else if ( this . _categories . length === 0 ) { return true ; } else { return false ; } } return false ; } private _getDataString ( data ) { return typeof data === 'string' ? data : JSON . stringify ( data ) ; } private _getTime ( ) { const now = new Date ( ) ; return now . toISOString ( ) ; } }