import { EventEmitter , Injectable , Optional } from "@angular/core" ; import * as log4javascript from "log4javascript" ; import { AjaxAppender } from "./ajax-appender.model" ; import { LocalStorageAppender } from "./local-storage-appender.model" ; import { LogLevelConverter } from "./log-level.converter" ; import { LogMessage } from "./log-message.model" ; import { Logger } from "./logger.model" ; import { LoggingServiceConfiguration } from "./logging-service.configuration" ; import { MemoryAppender } from "./memory-appender.model" ; /**
 * Service for logging functionality.
 *
 * By default, the following settings are used:
 *  - logger: root with level WARN
 *  - appender: BrowserConsoleAppender with threshold DEBUG and MemoryAppender with threshold ALL
 *
 * Via [configure](#configure), it is possible to amend these settings.
 */ @ Injectable ( { providedIn : "root" } ) export class LoggingService { /**
     * Event triggered when the log messages got (potentially) change.
     * This can happen when:
     * - new message was added
     * - all message where removed from memory
     * - all massages where removed for one spcific LocalStorageAppender
     */ public logMessagesChanged : EventEmitter < void > ; param message public ajaxAppenderFailed : EventEmitter < string > ; private memoryAppender : MemoryAppender ; private browserConsoleAppender : log4javascript . BrowserConsoleAppender ; /**
     * Creates a new instance of the service.
     */ constructor ( ) { log4javascript . logLog . setQuietMode ( true ) ; this . logMessagesChanged = new EventEmitter < void > ( ) ; this . ajaxAppenderFailed = new EventEmitter < string > ( ) ; const logger = log4javascript . getRootLogger ( ) ; logger . setLevel ( log4javascript . Level . WARN ) ; this . browserConsoleAppender = new log4javascript . BrowserConsoleAppender ( ) ; this . browserConsoleAppender . setLayout ( new log4javascript . PatternLayout ( "%d{HH:mm:ss,SSS} %c %m" ) ) ; this . browserConsoleAppender . setThreshold ( log4javascript . Level . ALL ) ; logger . addAppender ( this . browserConsoleAppender ) ; this . memoryAppender = new MemoryAppender ( ) ; this . memoryAppender . setLayout ( new log4javascript . PatternLayout ( "%d{HH:mm:ss,SSS} %c %m" ) ) ; this . memoryAppender . setOnLogMessagesChangedCallback ( ( message ) => { this . logMessagesChanged . emit ( ) ; } ) ; logger . addAppender ( this . memoryAppender ) ; this . configure ( ) ; } param configuration public configure ( configuration ? ) { if ( typeof configuration === "undefined" ) { configuration = { } ; } if ( typeof configuration . logLevels !== "undefined" ) { for ( const level of configuration . logLevels ) { let logger ; if ( level . loggerName === "root" ) { logger = log4javascript . getRootLogger ( ) ; } else { logger = log4javascript . getLogger ( level . loggerName ) ; } try { logger . setLevel ( LogLevelConverter . levelToLog4Javascript ( LogLevelConverter . levelFromString ( level . logLevel ) ) ) ; } catch ( e ) { throw new Error ( `invalid log level ${ level . logLevel }` ) ; } } } if ( typeof configuration . ajaxAppender !== "undefined" ) { const ajaxAppender = new AjaxAppender ( configuration . ajaxAppender ) ; ajaxAppender . appenderFailed . subscribe ( ( message ) => { this . ajaxAppenderFailed . emit ( message ) ; } ) ; log4javascript . getRootLogger ( ) . addAppender ( ajaxAppender ) ; } if ( typeof configuration . localStorageAppender !== "undefined" ) { const localStorageAppender = new LocalStorageAppender ( configuration . localStorageAppender ) ; log4javascript . getRootLogger ( ) . addAppender ( localStorageAppender ) ; const appenders = new Logger ( ) . getInternalLogger ( ) . getEffectiveAppenders ( ) ; const memoryAppender = appenders . find ( ( a ) => a . toString ( ) === "Ionic.Logging.MemoryAppender" ) as MemoryAppender ; if ( memoryAppender ) { log4javascript . getRootLogger ( ) . removeAppender ( memoryAppender ) ; log4javascript . getRootLogger ( ) . addAppender ( memoryAppender ) ; } } if ( configuration . memoryAppender ) { this . memoryAppender . configure ( configuration . memoryAppender ) ; } if ( configuration . browserConsoleAppender ) { if ( configuration . browserConsoleAppender . threshold ) { const convertedThreshold = LogLevelConverter . levelToLog4Javascript ( LogLevelConverter . levelFromString ( configuration . browserConsoleAppender . threshold ) ) ; this . browserConsoleAppender . setThreshold ( convertedThreshold ) ; } } } return public getRootLogger ( ) { return new Logger ( ) ; } param loggerName return public getLogger ( loggerName ) { return new Logger ( loggerName ) ; } return public getLogMessages ( ) { return this . memoryAppender . getLogMessages ( ) ; } param localStorageKey returns public getLogMessagesFromLocalStorage ( localStorageKey ) { return LocalStorageAppender . loadLogMessages ( localStorageKey ) ; } /**
     * Remove all log messages.
     */ public removeLogMessages ( ) { this . memoryAppender . removeLogMessages ( ) ; this . logMessagesChanged . emit ( ) ; } param localStorageKey public removeLogMessagesFromLocalStorage ( localStorageKey ) { LocalStorageAppender . removeLogMessages ( localStorageKey ) ; this . logMessagesChanged . emit ( ) ; } }